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);
Related
I could serialize the JsonPatchDocument model by using JsonConvert.SerializeObject(), but the type of result is string, how can I convert it to normal array type? Or how to get JsonPatchDocument object straight to array?
var pathSerialized = JsonConvert.SerializeObject(patch);
Console.WriteLine(pathSerialized);
// Result as string:
// "[{"value":"2018-08-30","path":"/openTo","op":"replace"},{"value":"2018-04-01","path":"/openFrom","op":"replace"}]"
You don't have to serialize the JsonPatchDocument object at all. You can access its properties directly through the object. For example filtering for the path property:
var elementsWithPath = patch.Operations.Where(o => o.path.Equals("some path"));
I think you might be looking to do something with JTokens from the Newtonsoft.Json.Linq namespace. You can turn the pathserialized string into a JToken with var jToken = JToken.Parse(pathSerializer), then explore the underlying objects and properties by enumerating them with var childTokens = jToken.Children().
One of those child tokens is going to be a JObject, which is a Json representation of an object. You can access properties of a JObject with jObject["propertyName"].
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);
I mostly work on the PHP , recently have switched to ASP.NET,
When parse the JSON, I can simply use -> to get the field, e.g.
foreach(json_decode($_POST['mandrill_events']) as $event) {
$event = $event->event;
$email_type = $event->msg->metadata->email_type;
}
However, in ASP.NET , there is no action, this is my attempt code
var post_data = Request.Form["mandrill_events"];
JavaScriptSerializer ser = new JavaScriptSerializer();
var post_data_json = ser.Deserialize<Dictionary<string, string>>(post_data);
foreach (var event_obj in post_data_json) {
//how to parse the event_obj?
}
Thanks a lot for helping.
use Newtonsoft Json.NET
JsonConvert.DeserializeObject<DataModel>(json);
Unless you want to write a C# class that represents the JSON you are POSTing (the safest solution), you can use the dynamic type to create an object which will look like your JSON. You can then do something like this answer to access the properties.
This solution doesn't give you type safety and the DLR will resolve the properties of the dynamic object at runtime.
As other answers have mentioned, your life will be made much easier by using Newtonsoft JSON which will allow you to write:
dynamic events = JsonConvert.DeserializeObject<dynamic>(post_data);
foreach(dynamic evt in events)
{
string emailType = evt.msg.metadata.email_type;
}
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
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.