How to deserialize this JSON array - c#

The JSON is coming in like this (pseudo code):
[{one-off intro object}, [{object},{object},{object}]]
So it's an array where the first thing in the array is an object I'm not interested in and the second is another array full of the objects I actually want to deserialize.
How can I do this with JSON.NET?

You can use Json.Net's LINQ-to-JSON API to do the job.
For sake of example, let's assume your JSON looks like this:
[
{
"blah": "nothing interesting here"
},
[
{
"id": 1,
"name": "foo",
"desc": "description of foo"
},
{
"id": 2,
"name": "bar",
"desc": "blurb about bar"
},
{
"id": 3,
"name": "baz",
"desc": "buzz about baz"
}
]
]
First, define a class to hold the items from the inner array that you're interested in.
public class Item
{
[JsonProperty("id")]
public int Id { get; set; }
[JsonProperty("name")]
public string Name { get; set; }
[JsonProperty("desc")]
public string Description { get; set; }
}
Now, all you need to do is parse the JSON to a JArray, then get the child array from that and use ToObject() to convert it to a list of items.
JArray ja = JArray.Parse(json);
List<Item> list = ja[1].ToObject<List<Item>>();
From there, you can use the list of items as you normally would.
Fiddle: https://dotnetfiddle.net/CaFzux

You can get to the second set of objects either via strongly typed casting or dynamically, here is an example of doing it dynamically:
dynamic jsonArray = JArray.Parse(json);
dynamic targetJsonObjects = jsonArray[1];

Have you tried just simple model like this?
public class RootObject
{
public object IntroObject { get; set; }
public List<Item> Items { get; set; }
}
public class Item
{
public string WhatEverPropertyYouNeed{ get; set; }
}

Related

Getting data in Json.Net c#

I want to get data from json file correctly. The json data file I modeled for this is as follows:
{
"Free title 1":[
{
"Subject": "a1",
"Relation": "a2"
},
{
"Subject": "b1",
"Relation": "b2"
}
],
"Another free title":[
{
"Subject": "z1",
"Relation": "z2"
},
{
"Subject": "y1",
"Relation": "y2"
}
],
"Unordered title":[
{
"Subject": "x1",
"Relation": "x2"
},
{
"Subject": "w1",
"Relation": "w2"
}
]
}
This is how I create an object class:
public class _Infos_
{
public List<_Info_> Infos { get; set; }
}
public class _Info_
{
public string Subject { get; set; }
public string Relation { get; set; }
}
And finally I'm trying to get the data in a method like this:
var js = JsonConvert.DeserializeObject<_Infos_>(File.ReadAllText("__FILE_PATH__"));
foreach (var j in js.Infos)
{
MessageBox.Show(j.Subject);
}
I get the error that js is empty. Here I want to get Free title 1, Another free title and Unordered title in a list. Of course, these titles will be constantly changing. Afterwards, I want to get the Subject and Relation data under these titles. But I have no idea how to get it.
This data structure is a dictionary of collections of _Info_s. You need to deserialize it to Dictionary<string, List<_Info_>>.
Here are System.Text.Json and Json.net examples:
var d = System.Text.Json.JsonSerializer.Deserialize<Dictionary<string, List<_Info_>>>(json);
var d2 = Newtonsoft.Json.JsonConvert.DeserializeObject<Dictionary<string, List<_Info_>>>(json);
Your class definition is a little wrong.
You can use online tools "json to c#" to generate the correct classes.
like this one: https://json2csharp.com
Your "root" of your json for example does not contain an array in your json. The property "Free title 1":[..] is an array, so your root needs a property with the name FreeTitle1 and it has to be an array/list.
public class Root
{
[JsonProperty("Free title 1")]
public List<TitleInfo> FreeTitle1 { get; set; }
[JsonProperty("Another free title")]
public List<TitleInfo> AnotherFreeTitle { get; set; }
[JsonProperty("Unordered title")]
public List<TitleInfo> UnorderedTitle { get; set; }
}
public class TitleInfo
{
public string Subject { get; set; }
public string Relation { get; set; }
}
If your object members have dynamic names, you can also manually deserialize the object, e.g. using the general type JObject. E.g.
JObject obj = JObject.Parse(File.ReadAllText("__FILE_PATH__"));
JObject implements IEnumerable<KeyValuePair<string, JToken>> over which you can iterate.
Each member will then have JToken Value, which is a JArray in this case, which you can cast to a List of your type.
foreach (var groups in obj)
{
var infos = groups.Value.ToObject<List<_Info_>>();
// .. loop over infos
}

How to query into a JSON getting using LINQ?

JSON
{
"count": 3,
"value": [
{
"id": "AAAAAAAAAAAAA",
"description": "test1",
"name": "name1"
},
{
"id": "BBBBBBBBBB",
"description": "test2",
"name": "name2"
},
{
"id": "CCCCCCCCCCCC",
"description": "test3",
"name": "name3"
}
]
}
I have a code in my solution retrieving from a LIST api and giving the JSON above.
How can I use a LINQ to retrieve specific values? (e.g) I need to select name1 and I will get the id,description,name values.
I am using a dynamic variable in my code:
dynamic json = JObject.Parse(client.GetString().Result);
I'd been tinkering with other online guides the past few hours. However, can't get the result right.
Please help.
One solution would be to deserialize your JSON string into C# objects and then use Linq to get a specific object.
C# class definitions:
public class Content
{
[JsonProperty("count")]
public int Count { get; set; }
[JsonProperty("value")]
public List<Value> Values { get; set; }
public Content()
{
Values = new List<Value>();
}
}
public class Value
{
[JsonProperty("id")]
public string Id { get; set; }
[JsonProperty("description")]
public string Description { get; set; }
[JsonProperty("name")]
public string Name { get; set; }
}
Deserializing and getting the object:
string json = #"{
""count"": 3,
""value"": [
{
""id"": ""AAAAAAAAAAAAA"",
""description"": ""test1"",
""name"": ""name1""
},
{
""id"": ""BBBBBBBBBB"",
""description"": ""test2"",
""name"": ""name2""
},
{
""id"": ""CCCCCCCCCCCC"",
""description"": ""test3"",
""name"": ""name3""
}
]
}";
Content content = JsonConvert.DeserializeObject<Content>(json);
Value value = content.Values.FirstOrDefault(x => x.Name.Equals("name1", StringComparison.InvariantCultureIgnoreCase));
First, you can create a class to represent a client:
public class Client
{
public string Id { get; set; }
public string Description { get; set; }
public string Name { get; set; }
}
With this class, you can use JObject.Parse (as you're already doing) to parse the JSON into something that can be queried, use SelectToken to pull out the value array and then use ToObject to convert that to a list of Clients. Here's what that looks like:
var jsonObject = JObject.Parse(json_source);
var jsonObjectValue = jsonObject.SelectToken("value");
var clients = jsonObjectValue.ToObject<List<Client>>();
Once you've got your clients variable, you can use a simple LINQ statement to find the one that is name1:
var clientWithName1 = clients.SingleOrDefault(x => x.Name == "name1");
In this case, clientWithName will be null if no such client was found.
Here's a dotnetfiddle that demonstrates a complete solution.
Create an object Client that has properties id, description and name. Deserialize the json into a list of these objects.
List<Client> clients = JsonConvert.Deserialize<List<Client>>(json_source);
string desc = clients[0].description;
Apparently with fiddling with my code I found an answer for myself.
Thanks for to the ones trying to help me for giving me ideas.
var requestWorkProcess = await client.GetStringAsync("my url");
var workProcessId = JObject.Parse(requestWorkProcess)["value"].Children<JObject>().FirstOrDefault(o => o["name"].ToString() == workProcess).GetValue("id");

Deserializing a list of objects with different names in JSON.NET

I'm getting my data from a website which returns a .json format that is quite unfamiliar to me. I've been looking for the solution for a couple of hours, and I must be using the terminology.
The json is formatted something like this:
[
{
"Foo": {
"name": "Foo",
"size": {
"human": "832.73kB",
"bytes": 852718
},
"date": {
"human": "September 18, 2017",
"epoch": 1505776741
},
}
},
{
"bar": {
"name": "bar",
"size": {
"human": "4.02MB",
"bytes": 4212456
},
"date": {
"human": "September 18, 2017",
"epoch": 1505776741
}
}
}]
I'm using Newtonsoft's JSON.NET, and I can't seem to be able to create a data structure that would allow me to deserialize it, since it's the array of classes with different names. Specifically the property names "Foo" and "bar" could differ at runtime. Property names elsewhere in the JSON hierarchy are known.
Assuming that only the names "Foo" and "Bar" are unknown at compile time, you can deserialize that JSON into a List<Dictionary<string, RootObject>>, where RootObject is a c# model I generated automatically using http://json2csharp.com/ from the JSON for the value of "Foo".
Models:
public class Size
{
public string human { get; set; }
public int bytes { get; set; }
}
public class Date
{
public string human { get; set; }
public int epoch { get; set; }
}
public class RootObject
{
public string name { get; set; }
public Size size { get; set; }
public Date date { get; set; }
}
Deserialization code:
var list = JsonConvert.DeserializeObject<List<Dictionary<string, RootObject>>>(jsonString);
Notes:
The outermost type must be an enumerable such List<T> since the outermost JSON container is an array -- a comma-separated sequence of values surrounded by [ and ]. See Serialization Guide: IEnumerable, Lists, and Arrays.
When a JSON object can have arbitrary property names but a fixed schema for property values, it can be deserialized to a Dictionary<string, T> for an appropriate T. See Deserialize a Dictionary.
Possibly bytes and epoch should be of type long.
Working .Net fiddle.

In C#, how do I model a JSON object with multiple nested arrays?

I am getting this JSON response from a system I am connecting to and trying to figure out the best way to deserialize it into a C# object. I am currently using RestSharp which seems pretty straight forward to use but the format of the JSON is baffling me a bit. Here is the format that its coming in as:
[
{"name": "Tickets:",
"schema": [
{"dataType": "string", "colName": "First", "idx": 0},
{"dataType": "string", "colName": "Second", "idx": 1},
{"dataType": "string", "colName": "Name", "idx": 2}
],
"data": [
["bill", "test", "joe"],
["bill2", "test2", "joe2"],
["bill3", "test3", "joe3"]
]
}
]
Here is my current code:
var url = "http://myUrl:10111";
var client = new RestClient { BaseUrl = url };
var request = new RestRequest { Method = Method.GET, Resource = "/search?fmt=Json", RequestFormat = DataFormat.Json };
request.AddHeader("accept", "application/json");
var response = client.Execute(request);
var wptResponse = new JsonDeserializer().Deserialize<TicketResults>(response);
return wptResponse;
but as stated above I am trying to figure out the correct way to model the TicketResults object to support deserializing this message above.
Ideally I would like something like this:
public class TicketResults
{
public List<Ticket> Tickets {get;set;}
}
public class Ticket
{
public string First {get;set;}
public string Second {get;set;}
public string Name {get;set;}
}
and in this example above would get three entries in the Tickets collection.
Also, is the above JSON format normal as i have never seen this broken out into separate schema and data section (I can see where it might save some space but in this case the messages are not that big)
In Visual Studio 2012 and up and you can go to Edit > Paste Special > Paste JSON as classes. It produces the following code given your example pasted from clipboard.
public class Rootobject
{
public Class1[] Property1 { get; set; }
}
public class Class1
{
public string name { get; set; }
public Schema[] schema { get; set; }
public string[][] data { get; set; }
}
public class Schema
{
public string dataType { get; set; }
public string colName { get; set; }
public int idx { get; set; }
}
string json = File.ReadAllText("json.txt");
Rootobject root = new Rootobject();
root.Property1 = JsonConvert.DeserializeObject<Class1[]>(json);
I agree the json format is quite ... goofy. Here's how to model your dto:
public class JsonDto
{
public string name { get; set; }
public Schema[] schema {get; set;}
public string[][] data { get; set; }
}
public class Schema
{
public string dataType { get; set; }
public string colName { get; set; }
public int idx { get; set; }
}
I was able to get your string (unaltered) to deserialize with JSON.Net like this:
var jsonDto = JsonConvert.DeserializeObject<JsonDto[]>(json);
Let me know if you're still having trouble.
Do you have any control over the structure of the JSON being returned? It's kind of wacky. For some reason the field names and the data is separated out. If the format was a little more sensible like:
[
{
"First": "bill",
"Second": "test",
"Name": "joe"
},
{
"First": "bill2",
"Second": "test2",
"Name": "joe2"
},
]
Then you would have a shot at serializing it to your Ticket class. However, without reworking the JSON structure, which I don't recommend you do, the C# class that you are serializing to will have to match the JSON structure.
I suppose you could come up with an intermediary class to hold the JSON data as it comes to you. Then you could loop over those objets and create instances of the Ticket class out of them. At least that way you end up with a data structure you can work with.

JSON Can't be Deserialized to Object, Needs Array?

I am trying to take the incoming JSON items and bind them to listbox items but I am told by visual studio that I need to do an Array and not Object? I've never had to do this... Anyone know how?
My RootObject:
public class RootObject
{
public string url { get; set; }
public string display { get; set; }
public List<string> genetics { get; set; }
public List<string> price { get; set; }
public List<string> brandMaker { get; set; }
public string form { get; set; }
public string dosornos { get; set; }
public string qty { get; set; }
public string mfg { get; set; }
public string mobURI { get; set; }
}
Note: Genetics, Price, BrandMaker don't actually return anything but a value, like below:
"genetics": [
"typeophere"
],
"price": [
"$1400"
],
JSON FILE/REQUEST BASIC RESULT:
[
{
"url": "N/A",
"display": "",
"genetics": [
"microogiz"
],
"price": [
"96.016"
],
"brandMaker": [
"Oshi Kunti Multikashi, Osaka, JP"
],
"form": "tangent",
"dosornos": "n/a",
"qty": "88G",
"mfg": "SelfMade Industries, USA Dist.",
"mobURI": "n/a"
}
]
My original code:
// Get JSON via WEB
string ProviderURI = goURI;
webClient webClient = new WebClient();
webClient.DownloadStringCompleted += new
DownloadStringCompletedEventHandler(webClient_DownloadStringCompleted);
webClient.DownloadStringAsync(new Uri(ProviderURI));
void webClient_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
{
if (e.Error != null)
{
return;
}
var deserializedJSON = JsonConvert.DeserializeObject<RootObject>(e.Result);
lstBoxResults.ItemsSource = deserializedJSON; // or deserializedJSON.url.ToString();
}
Ok, you mention that genetics and price are arrays, but the returned JSON only contains one item. The rest of your RootObject are simple string properties.
Since you did not post a raw JSON example. Let me present a trimmed down version, to keep things simple and short.
{
"url": "http://www.stackoverflow.com",
"display": "This is a test",
"genetics": [
"typeophere"
],
"price": [
"$1400"
],
"form": "a form"
}
Now we can trim down the RootObject class type. I used Pascal casing for my properties and attributed them with JSON.NET attributes to assist with the deserialing / serializing.
[JsonObject(MemberSerialization = MemberSerialization.OptIn)]
public class RootObject
{
[JsonProperty(PropertyName = "url")]
public string Url { get; set; }
[JsonProperty(PropertyName = "display")]
public string Display { get; set; }
[JsonProperty(PropertyName = "genetics")]
public List<string> Genetics { get; set; }
[JsonProperty(PropertyName = "price")]
public List<string> Price { get; set; }
[JsonProperty(PropertyName = "form")]
public string Form { get; set; }
}
Since I don't know the web service you are calling I just stored this JSON in a text file. Marked it as an embedded resource and read it from there.
string json;
var resource = Application.GetResourceStream(new Uri("json.txt", UriKind.Relative));
using (var reader = new StreamReader(resource.Stream))
{
json = reader.ReadToEnd();
}
Just replace this part with your WebClient call to obtain your JSON data.
Now we can deserialize the JSON into a RootObject instance.
var rootObject = JsonConvert.DeserializeObject<RootObject>(json);
Works as advertised. Now you need to bind it to a ListBox control. If you hover over the ItemSource property of your ListBox instance you'll see that the tooltip mentions that you can bind it to a collection. Well a single rootObject isn't a collection. You can't bind it directly.
lstBoxResults.ItemsSource = rootObject;
This will NOT work. You cannot convert a RootObject instance to a System.Collections.IEnumerable. Which is what ItemsSource is expecting.
Easy fix. Let's create a collection.
lstBoxResults.ItemsSource = new List<RootObject> { rootObject };
This is assuming that your JSON data only returns one rootObject. Only one item will appear in your ListBox.
Now let's suppose your JSON data returns an array of RootObjects. For example an array called "items" which contains a collection of RootItems.
{
"items": [
{
"url": "http://www.stackoverflow.com",
"display": "This is a test",
"genetics": [ "typeophere" ],
"price": [ "$1400" ],
"form": "a form"
},
{
"url": "https://github.com/geersch",
"display": "This is another test",
"genetics": [ "typeophere" ],
"price": [ "$2000" ],
"form": "another form"
}]
}
Deserialing this is equally easy. Using JSON.NET obtain the collection of RootObjects.
var data = JObject.Parse(json)["items"];
Now deserialize into a collection (IEnumerable).
var rootObjects = JsonConvert.DeserializeObject<IEnumerable<RootObject>>(data.ToString());
Since you now already have a collection of RootObject there is no need to create one yourself. You can directly bind it to the ListBox.
lstBoxResults.ItemsSource = rootObjects;
Seems like the JSON data you receive is invalid. Just before parsing it make sure you modify it so that you have a valid JSON object.
For example:
json = json.Substring(json.IndexOf("[") + 1);
json = json.Substring(0, json.LastIndexOf("]"));
var rootObjects = JsonConvert.DeserializeObject<RootObject>(json);
Try this
RootObject rootObject;
if (json.startsWith("["))
{
rootObject = JsonConvert.DeserializeObject<List<RootObject>>(json)[0];
}
else
{
rootObject = JsonConvert.DeserializeObject<RootObject>(json);
}

Categories