fill json with another json [closed] - c#

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 days ago.
Improve this question
I have a json with this structure:
{
"HeaderFields": [
{
"Key": "Header1",
"Value": "hghghghghghghghghghg"
}
],
"SecondaryFields": [
{
"Key": "Secondary1",
"Value": "dgfhfhf"
}
]
}
}
then I have a dictionary in json format like this:
{
"Header1": "hghghghghghghghghghg",
"Secondary1": "hghghghghghghghghghgh"
}
How can I fill the Value property of each array that corresponds to the Key property of the dictionary key from the first json?

Related

How to sort by name an Enum in c# [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 months ago.
Improve this question
I was trying to sort an Enum by the names, I have the following code:
Enum.GetNames(x.options).Where(.....).Select(.....)
notice that x.options is an enum
and I would like to sort de results of the linq by the name of the Enum, how could I do it?
Assuming the following enum:
public enum Number
{
One = 1,
Two = 2,
Three = 3,
Four = 4,
}
You need to pass the enum's type in Enum.GetNames and then call OrderBy (to sort in ascending order):
var sortedEnum = Enum.GetNames(typeof(Number)).OrderBy(x => x);
// or, from .NET 5 onwards:
var sortedEnum = Enum.GetNames<Number>().OrderBy(x => x);
This will return an IOrderedEnumerable<string> having the following (sorted) values:
Four
One
Three
Two
Of course you could .ToList() or .ToArray() the results, to return them in a list or an array, respectively.

get a value from an object property and assign it to a C # string value [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
The community is reviewing whether to reopen this question as of 2 years ago.
Improve this question
I have a below code to capture an objects values:
var key = fulfillment.GetType().GetProperties().FirstOrDefault(p => p.Name.ToLower().Contains("operator")).GetValue(fulfillment);
the code return:
the Operator property type is:
[JsonProperty(PropertyName = "operator")]
public object Operator { get; set; }
i want to get the name value of the index 1 -> OMS_OPERATOR_AUTOMATED and assign it to another string variable. How can i do this ?
Final answer after looking at code and data structure the answer was:
var foundOperator = (Dictionary<string, object>) fulfillment.Operator;
var teste = foundOperator["name"];

Convert object values to base64 [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
I am very new to C#.
encodevalues(object)
I need to use above function where i have to encode the values of object. Object can be different every time like below
{ "name" "Swamy", "email": : "Swamy123#gmail.com }
or
{"firstname": "Swamy", "lastname": "reddy"}
or
{"name": "Swamy"}
So I want to encrypt the only values and return the object.
How can i do this, please help.
Best,
It seems like you want a function that encodes all string properties of any given object.
To do this, you can use a bit of reflection. Note however, that the function below performs the encoding in-place. If you need it to return an encoded copy of the original object, you'll need to do some cloning (which can get pretty complicated if you need deep cloning).
public static void EncodeAllProperties(object obj)
{
var props = obj.GetType().GetProperties();
foreach (var prop in props)
{
if (prop.PropertyType == typeof(string))
{
prop.SetValue(obj, Base64Encode(prop.GetValue(obj) as string));
}
}
}
// From https://stackoverflow.com/a/11743162/11981207
public static string Base64Encode(string plainText)
{
var plainTextBytes = System.Text.Encoding.UTF8.GetBytes(plainText);
return System.Convert.ToBase64String(plainTextBytes);
}

Sort student array according to average marks [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I have created student class array in C# like this
var Student = new Student[5];
having variables sid,name, avgMrks;
I want to sort array according to average marks of all the students.
I assuming when you say having variables, you mean that the Student object has the properties: sid, name, avgMrks. You can do:
Student.OrderBy (x=>x.avgMrks);
Use LINQ
Student = Student.OrderByDescending(c => c.avgMrks).ToArray();
It returns IOrderedIEnumerable, which you can convert back to Array if you want.
Or
string[] ArrStr = new string[] { "A", "A2", "A1" };
Array.Sort(ArrStr);
Array.Reverse(ArrStr);

Getting individual string length in an array [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I am trying to write a program which does the following:
There is a method called lengthOffTheWords. It receives an array of strings, and returns an array of numbers which represent the length of each individual string.
Ex: For the following input
{"I", "know", "a" , "friend"} the method returns {1,4,1,6} .
{"yes"} the method returns {3}.
{"me", "too"} the method returns {2,3}.
I would like to see an example of how to write it.
I would do something like this:
public int[] LengthOffTheWords(string[] array)
{
return array.Select(item => item.Length).ToArray();
}
I did not test this but it should do what you want.
int[] GetLengths(string[] array)
{
int[] structure = new int[array.Length];
for(int i = 0;i < array.Length;i++)
{
int length = array[i].Length;
structure[i] = length;
}
return structure;
}

Categories