It's my first work with Json.
I've already installed Json.Net in my Visual Studio project and used to deserialize some simple string like this:
{
"A":"1",
"B":"2",
"C":"3"
}
With this code:
JToken token = JObject.Parse("{ "A":"1","B":"2","C":"3"}";
string aValue = token.SelectToken("A");
string aValue = token.SelectToken("B");
string aValue = token.SelectToken("C");
But I don't know how to do with a Json like this:
{
"LIST":[
{
"A":"value1",
"B":"value1",
"C":"value1"
}
{
"A":"value2",
"B":"value2",
"C":"value2"
}
{
"A":"value3",
"B":"value3",
"C":"value3"
}
],
"D":"value4",
"E":"value5",
"F":"value6"
}
How can get all elements of type and the other variable like D, E and F?
Thank you
The easiest way would be to create objects and deserialize into those:
public class Parent
{
public Child[] LIST { get; set; }
public string D { get; set; }
public string E { get; set; }
public string F { get; set; }
}
public class Child
{
public string A { get; set; }
public string B { get; set; }
public string C { get; set; }
}
Once you have your classes defined, deserializing your JSON is as easy as:
var p = JsonConvert.DeserializeObject<Parent>(json);
#Justin's answer is good and will work well. However, if you want to continue using the LINQ-to-JSON API style as you are doing now, here is how you can get all the info:
JToken token = JToken.Parse(jsonString);
foreach (JToken t in token["LIST"])
{
Console.WriteLine(t["A"]);
Console.WriteLine(t["B"]);
Console.WriteLine(t["C"]);
}
Console.WriteLine(token["D"]);
Console.WriteLine(token["E"]);
Console.WriteLine(token["F"]);
Related
I have this json string:
[
{
"id":"EORDERING_GRE017",
"name":"DELIMITER",
"value":"|"
},
{
"id":"EORDERING_GRE017",
"name":"ENABLED",
"value":"Y"
},
{
"id":"EORDERING_GRE017",
"name":"EXTERNALERRORRECIPIENT",
"value":"jaymie#moo.co.uk; matt#moo.co.uk"
},
{
"id":"EORDERING_GRE017",
"name":"FILETYPE",
"value":"delimited"
},
{
"id":"EORDERING_GRE017",
"name":"INTERNALERRORRECIPIENT",
"value":"jaymie#moo.co.uk; matt#moo.co.uk"
},
{
"id":"EORDERING_GRE017",
"name":"USESOWNBRANCHCODES",
"value":"True"
},
{
"id":"EORDERING_GRE017",
"name":"USESOWNSKUS",
"value":"True"
}
]
And I would like to turn that json into my class, which looks like this:
public class Settings
{
public bool Enabled { get; set; }
public string FileType { get; set; }
public string Delimiter { get; set; }
public string OrderFileSuffix { get; set; }
public string ResultFileSuffix { get; set; }
public bool UseOwnBranchCodes { get; set; }
public bool UseOwnProductCodes { get; set; }
public string InternalContacts { get; set; }
public string ExternalContacts { get; set; }
}
But I am unsure which is the best way to do this. Can someone give me a hand? :)
You could create a NameValue object:
public class NameValuePair
{
public string Name { get; set; }
public string Value { get; set; }
}
And deserialize the json array to List and convert it to Dictionary:
var dict = JsonConvert.DeserializeObject<List<NameValuePair>>(json).ToDictionary(x => x.Name, x => x.Value);
Then create a custom converter class with a method that accepts this dictionary and returns a Settings object:
public class SettingsConverter
{
public Settings Convert(IDictionary<string, string> data)
{
return new Settings
{
Enabled = data["ENABLED"].Equals("Y", StringComparison.Ordinal),
...
};
}
}
The JSON is an array of objects that would fit this class:
public class Setting
{
public string Id { get; set; }
public string Name { get; set; }
public string Value { get; set; }
}
So deserialize into that:
var settingList = JsonConvert.DeserializeObject<Setting[]>(jsonString);
But then you want to map specific settings to specific properties of that Settings class. You could do that by trying to find the particular setting for each property in the list of settings:
var settingsObject = new Settings
{
FileType = settingList.FirstOrDefault(s => s.Name == "FILETYPE")?.Value,
Delimiter = settingList.FirstOrDefault(s => s.Name == "DELIMITER")?.Value,
// ...
}
You'll have to manually map the properties. If you don't want that, you could write a custom serializer, but that'll quickly become a maintenance nightmare.
For those out there that may need some more help with the JSON Class Configuration, try: http://json2csharp.com/#
An excellent way of Auto Generating the Classes!
Or even easier, in VS, Goto:
Edit -> Paste Special -> Paste as JSON Classes
I have a Json of type :
{
"JobProcessors": [
{
"JobName": "ArchivalJob",
"IsEnabled": true,
"Batching": {
"BatchSize": 0,
"DegreeOfParallelism": -1
},
"Settings": {
"ArchivalJobCollectionPageSize": 50
}
},
{
"JobName": "AuditLogJob",
"IsEnabled": false,
"Batching": {
"BatchSize": 10,
"DegreeOfParallelism": -1
},
"Settings": {}
}
],
"ScheduledJobs": [
{
"JobName": "RemoteStartClientCommandJob",
"PrimaryAction": {
"ConnectionString": "#JobProcessorsIntegrationSBConnectionStringValue#",
"Settings": {
"LeadTimeInSeconds": "600",
"MaxSrsJobCount": 25
}
},
"ErrorAction": {
"ConnectionString": "#PairedJobProcessorIntegrationSBConnectionStringValue#",
"EntityPath": "remotestartqueue",
"Settings": {
"LeadTimeInSeconds": "600",
"MaxSrsJobCount": 25
}
}
}
]
}
I want to check the "IsEnabled" property for all "JobName" for which come under "JobProcessors" category.
In C# what i Have used till now is :
dynamic parsedJson = JsonConvert.DeserializeObject(reader.GetString(1));
foreach (var item in parsedJson)
{
foreach (var smallitem in item)
{
foreach (var tag in smallitem)
{
if(tag.IsEnabled.toString()=="true"){
Console.WriteLine("true");
}
}
}
}
This is giving me correct result except the fact that it also iterates for "ScheduledJobs" . But the main issue is :
Is this the right or most efficient way to do this ? If possible suggest some better method .
One that i know of is using classes , but i may not know the json structure beforehand. Also the json is very huge so making classes can be cumbersome !!
Given that you are already doing JObject.Parse(jsonstring); to parse your JSON string, you can use SelectTokens() with a JSONPath query to find all "JobName" objects under "JobProcessors":
// I want to check the "IsEnabled" property for all "JobName" for which come under "JobProcessors"
foreach (var job in root.SelectTokens("..JobProcessors[?(#.JobName)]"))
{
var isEnabled = (bool?)job["IsEnabled"];
Debug.WriteLine(string.Format("Job {0}: IsEnabled={1}", job["JobName"], isEnabled));
}
Notes:
.. is the recursive descent operator: it recursively descends the JToken hierarchy returning each item, subsequently to be matched against the remaining parts of the query string.
JobProcessors returns values of properties of that name.
[?(#.JobName)] returns array items (of JobProcessors in this case) that are objects with a JobName property.
(bool?) casts the value of "IsEnabled" to a boolean or null if missing.
And the output of this is:
Job ArchivalJob: IsEnabled=True
Job AuditLogJob: IsEnabled=False
As in your code snippet we are using two foreach it may take time for large object. So we can do the same thing in a single foreach or if you have some specific node to fetch or search we can use linq, and for this first we need to convert our json object into c# object. For converting Json object to C# you can use this site "http://json2csharp.com/" then we can Deserialize Json object into c#.
It will be something like this
string jsonString = "your Json Object as string";
var jsonObject = JsonConvert.DeserializeObject<RootObject>(jsonString);
foreach (JobProcessor obj in jsonObject.JobProcessors)
{
string JobName = obj.JobName;
bool value=obj.IsEnabled;
}
And I also converted given Json in c# object if the Json object is same you can directly use these classes.
public class Batching
{
public int BatchSize { get; set; }
public int DegreeOfParallelism { get; set; }
}
public class Settings
{
public int ArchivalJobCollectionPageSize { get; set; }
}
public class JobProcessor
{
public string JobName { get; set; }
public bool IsEnabled { get; set; }
public Batching Batching { get; set; }
public Settings Settings { get; set; }
}
public class Settings2
{
public string LeadTimeInSeconds { get; set; }
public int MaxSrsJobCount { get; set; }
}
public class PrimaryAction
{
public string ConnectionString { get; set; }
public Settings2 Settings { get; set; }
}
public class Settings3
{
public string LeadTimeInSeconds { get; set; }
public int MaxSrsJobCount { get; set; }
}
public class ErrorAction
{
public string ConnectionString { get; set; }
public string EntityPath { get; set; }
public Settings3 Settings { get; set; }
}
public class ScheduledJob
{
public string JobName { get; set; }
public PrimaryAction PrimaryAction { get; set; }
public ErrorAction ErrorAction { get; set; }
}
public class RootObject
{
public List<JobProcessor> JobProcessors { get; set; }
public List<ScheduledJob> ScheduledJobs { get; set; }
}
Hope this will help.
Thank you
I am querying Firebase and retrieve a collection of objects like so:
{"-K5f0ccEKkVkxTAavQKY": {
"Appeal": {
"ID": "1450273330435",
"comps": [
162248,
162272,
162273,
162281,
162544
],
"property": {
"Address": "15 Main Street",
"propID": 169729
},
"timeDateStamp": "Wed Dec 16 2015 08:42:10 GMT-0500 (Eastern Standard Time)",
"userUUID": "google:229139952703238437512",
"year": 2016
}
}}
I would like to deserialize them into objects with this definition:
public class Appeal
{
public string ID;
public List<string> comps;
public AppealProperty property;
public string timeDateStamp;
public string UUID;
public int year;
}
public class AppealProperty
{
public string address;
public string propID;
}
I have troubles getting it deserialized. I don't need the initial string (e.g. "K5f0ccEKkVkxTAavQKY"). I'm able to change the object definitions if need be. I have a feeling a Dictionary would be useful.
The quick and dirty object is to use Dictionary<string,Appeal> as your deserialization target. At that point it would be as simple as:
var firebaseLookup = JsonConvert.DeserializeObject<Dictionary<string,Appeal>>(json);
var data = firebaseLookup.Values.ToList(); // or FirstOrDefault();
This approach would also handle the case if you ever had to get multiple objects at once, and it would give you the opportunity to use that key if it turns out the key was important after all.
You could serialise your data into the classes below.
public class AppealProperty
{
public string Address { get; set; }
public int propID { get; set; }
}
public class Appeal
{
public string ID { get; set; }
public List<int> comps { get; set; }
public AppealProperty property { get; set; }
public string timeDateStamp { get; set; }
public string userUUID { get; set; }
public int year { get; set; }
}
public class FireBase
{
public Appeal Appeal { get; set; }
}
public class RootObject
{
[JsonProperty(PropertyName = " - K5f0ccEKkVkxTAavQKY")]
public FireBase FireBaseRoot
{
get;
set;
}
}
Assuming that you are using JSON.NET, you can then get the object you are after, using this snippet:
var firebaseObject = JsonConvert.DeserializeObject<RootObject>(json);
var data = firebaseObject.FireBaseRoot.Appeal;
If the root name is dynamic, as indicated by your comment, you could skip the root instead and serialise straight into the FireBase class:
JObject parsedJson = JObject.Parse(json);
var fireBase = parsedJson.First.Children().First().ToObject(typeof (FireBase));
Since I've never been able to parse a DataSnapshot with newtonSoft Json parser, I did this to build a list of object I needed to put in a ListView:
MyModelObject class
public class MyModelObject: Java.Lang.Object
{
public string Title { get; set; }
public string Description { get; set; }
public MyModelObject(){}
}
into My Listener
public void OnDataChange(DataSnapshot snapshot)
{
List<MyModelObjecct> myList = new List<MyModelObject>();
myList = databaseService
.GetMyModelObjectList(snapshot
.Children?
.ToEnumerable<DataSnapshot>());
}
Method into the DatabaseService class
public List<MyModelObject> GetMyModelObjectList(IEnumerable<DataSnapshot> enumerableSnapshot)
{
List<MyModelObject> list = new List<MyModelObject>();
foreach (var item in enumerableSnapshot)
{
list.Add(ObjectExtensions.DataSnapshotToObject<MyModelObject>(item.Children?.ToEnumerable<DataSnapshot>()));
}
return list;
}
ObjectExtensions class
public static class ObjectExtensions
{
public static T DataSnapshotToObject<T>(IEnumerable<DataSnapshot> source)
where T : class, new()
{
var someObject = new T();
var someObjectType = someObject.GetType();
foreach (var item in source)
{
someObjectType
.GetProperty(item.Key)
.SetValue(someObject, item.Value.ToString(), null);
}
return someObject;
}
}
I make a web request in Silverlight for windows phone. And this is the responce.
{"result": {
"account": null,
"checkInfo": null,
"command": null,
"shifts": [
{
"description": "17:45 - 17:55 work shift",
"id": 5459,
"venueId": 132
}]}}
I use Newtonsoft.Json.dll and my purpose is to catch the array - shifts.
JObject obj = JObject.Parse(StringShifts);
JArray sh = (JArray)obj["shifts"];
But every time sh value is null. What i'm i doing wrong? Thank you in advance.
The other way around is: (This is very helpful, if you are doing more operations like this in your project)
Create these classes in your project
public class Shift
{
public string description { get; set; }
public int id { get; set; }
public int venueId { get; set; }
}
public class Result
{
public object account { get; set; }
public object checkInfo { get; set; }
public object command { get; set; }
public List<Shift> shifts { get; set; }
}
public class RootObject
{
public Result result { get; set; }
}
And then in your code
var rootObject = JsonConvert.DeserializeObject<RootObject>(StringShifts);
foreach(var shift in rootObject.result.shifts)
{
Console.Write(shift.description);
}
This way you can have more control on your json response data. But L.B 's answer if it is one time process in your app.
var obj = (JObject)JsonConvert.DeserializeObject(json);
foreach (var shift in obj["result"]["shifts"])
{
Console.WriteLine((string)shift["description"]);
}
You are missing the root results node; this is how you should use it:
JArray sh = (JArray)obj["result"]["shifts"];
Also, do note that there is a missing } in the end of your JSON sample above!
So I am trying to parse some JSON that is returned to me by a third party api that looks something like:
{
"status":"ok",
"links":
[
{
"link":
{
"link_name":"Sample",
"link_id":"9999"
}
},
],//and so on with other nested properties
I have created classes to map the JSON to
[DataContract]
public class JsonTestResults
{
[DataMember]
public string status { get; set; }
[DataMember]
public IEnumerable<Link> links { get; set; }
}
[DataContract]
public class Link
{
[DataMember]
public string link_name { get; set; }
[DataMember]
public string link_id { get; set; }
}
And I'm pushing the response through this deserializer (taken from this post
public T Deserialise<T>( string json )
{
T obj = Activator.CreateInstance<T>( );
using (MemoryStream ms = new MemoryStream( Encoding.Unicode.GetBytes( json ) ))
{
DataContractJsonSerializer serializer = new DataContractJsonSerializer( obj.GetType( ) );
obj = (T)serializer.ReadObject( ms );
return obj;
}
}
However, my deserialized results are showing the contents of Link[] as null. (there is a Link object for each one returned, but the link_name and link_id are null.)
I've checked out this, this, this, this and this, but haven't been able to solve this issue. I am looking for a solution that doesn't require a third party library. (per my lead dev).
I don't believe it's a problem with the classes matching the JSON, but I can post the full code if anyone would like to review it.
You need one more class to deserialize it correctly
public class JsonTestResults
{
public string status { get; set; }
public IEnumerable<TempLink> links { get; set; }
}
public class TempLink
{
public Link link;
}
public class Link
{
public string link_name { get; set; }
public string link_id { get; set; }
}
I tested it with Json.Net and worked.
var obj = JsonConvert.DeserializeObject <JsonTestResults>(json);
JavaScriptSerializer also works
var obj2 = new JavaScriptSerializer().Deserialize<JsonTestResults>(json);
Its a syntax error in the JSON,
In a JSON Array the last element does not have a ',' after it.
{
"status":"ok",
"links":
[
{
"link":
{
"link_name":"Sample",
"link_id":"9999"
}
}
],
This will work !!