C# Json deserialize - property as variable - c#

I have problem. From json: https://api.csgofast.com/price/all I would like to get property name as variable. Can I do it with Newtonsoft Json library? Thanks in advance for help.

You can deserialize this JSON into a Dictionary<string, decimal> like this:
var prices = JsonConvert.DeserializeObject<Dictionary<string, decimal>>(json);

Related

C# Serializing an objects into JSON but retain the name

So I'm trying to serialize a Dictionary:
Dictionary<string, List<MyClass>>
My class contains a property called MyPropery
However the string comes out as "{"List1":[{"MyProperty":[]}]}"
How would I be able to make it convert to {"List1":["MyClass":{"MyProperty":[]}]}
To achieve what you want, you would need something like:
var obj = new Dictionary<string, List<Dictionary<string, MyClass>>>();
As kalimag pointed out, ["attr": "value"] isn't a valid JSON. The serialization of the object obj, illustrated above, could yield something like:
{"List1": [{"MyClass": {"MyProperty": []}}]}
Which is a valid JSON.

Working with a string in JSON format

I have a long string in JSON format. This is what it looks like:
{"ShowMapUrl":true,"GameDiffusionType":5,"InputYellowCards":false,"DisplayYellowCards":false,"InputYellowCards2":false}
Note that the string is much longer. I've been trying to convert that string into a dictionary by using json.NET without success. Any tips?
Use JsonConvert.DeserializeObject<Dictionary<string, object>>():
var json = #"{""ShowMapUrl"":true,""GameDiffusionType"":5,""InputYellowCards"":false,""DisplayYellowCards"":false,""InputYellowCards2"":false}";
var dict = JsonConvert.DeserializeObject<Dictionary<string, object>>(json);

How to parse a list of Integers from a JSON object

I'm using "Newtonsoft.Json.Linq.JObject" in my application.
I have a method that receives a JObject in the format:
{
"PersonnelIds": "[31,32,33,34]"
}
And I want to parse the content of PersonnelIds to a List of Integers.
What is the best way of doing that?
I can see that the values of the PersonnelIds is written as string "[31,32,33,34]" so to parse it with this syntax you can use the following code
JObject jObject = JObject.Parse(myjson);
JToken jToken = jObject.GetValue("PersonnelIds");
var array = JArray.Parse(jToken.Value<string>()).Select(x => (int)x).ToArray();
if your value is not string so your JSON is like {"PersonnelIds": [31,32,33,34]
} then you can parse it using the following code
JObject jObject = JObject.Parse(myjson);
JToken jToken = jObject.GetValue("PersonnelIds");
int[] array = jToken.Values<int>().ToArray();
Create a class to deserialize your json:
To create classes, you can copy the json in clipboard and use the
Edit / Paste special / Paste JSON as class
in visual studio (I use vs2013).
Then deserialize your string.
See my solution on this post

c# - deserialization of specific JSON

I have JSON text with structure like this:
{
"324523":{"a":1345, "b":2344},
"134565":{"a":1642, "b":2322},
"123426":{"a":1556, "b":2674},
...
}
Is it possible to make .NET classes to deserialize such JSON? It looks like a Dictionary, but DataContractJsonSerializer doesn't deserialize this.
Try JSON.Net, which you can obtain from NuGet, or from the JSON.Net website.
Deserializing:
var jsonValues = JsonConvert.DeserializeObject(someJsonString);
var pocoValue = JsonConvert.DeserializeObject<YourClass>(someJsonString);
// or as a dictionary:
var dictionary = JsonConvert.DeserializeObject<Dictionary<MyClass>>(someJsonString);

How to deserialize JSON to Dictionary using JSON.NET?

I am querying data from http://www.imdbapi.com and would like to parse the result using Json.net library. Could someone please tell me how I can use this library to convert the query response into a Map<string, string>.
With this code I'm able to get all keys, but how can query the values then?
JObject obj = JObject.Parse(response);
IList<string> props = obj.Properties().Select(p => p.Name).ToList();
Try JSON.NET
Just use this:
Dictionary<string, string> movieValues =
JsonConvert.DeserializeObject<Dictionary<string, string>>(responseFromImdbApi);
Just get the values like this:
movieValues["title"]
movieValues["released"]
movieValues["genre"]
Why would you use an external library when it is already available ?
JavaScriptSerializer works great.

Categories