C# deserializing weirdly formatted JSON array - c#

I'm working on a client library for an accounting software. Communication is done via HTTP API, which return JSON responses. Till now JSON has been quite ordinary and translates easily into classes for Json.Net parsing. But there is a query, where it returns a very weird array - each array member has a title (which has no importance) and is treated like different object (100% identical members). Last time I was dealing with it, I wrote a little helper to remove the keys and replaced {} with [], but I wonder, if I can use Json.NET directly to parse this correctly.
I'm using latest stable Json.Net, .Net Framework 4.6 and deserialize using JsonConvert.DeserializeObject.
Json array, which doesn't parse
{
...
"warehouses": {
"1": {
"warehouseID": 1,
"totalInStock": 0,
...
},
"2": {
"warehouseID": 2,
"totalInStock": 0,
...
},
"3": {
"warehouseID": 3,
"totalInStock": 0,
...
},
"4": {
"warehouseID": 4,
"totalInStock": 0,
...
}
},
...
}
Classes
public class MainObject{
...
public List<Warehouse> warehouses { get; set;}
...
}
public class Warehouse{
public int warehouseID { get; set; }
public string totalInStock { get; set; }
...
}
'Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'System.Collections.Generic.List`1[Warehouse]' because the type requires a JSON array (e.g. [1,2,3]) to deserialize correctly.

Thats not an array, its a key-value map. The corresponding class in C# would be a Dictionary<TKey,TValue>.
Refactor into the following:
public class MainObject{
...
public Dictionary<int, Warehouse> warehouses { get; set;}
...
}

Related

How to deserialize a collection with a collection with Newtonsoft.json?

I have some Json content that contains an array, where each item contains an array of objects. I'm searching for a way to convert this into a list of objects. It is only the object with id and biCode is what I'm after, their parent is of no importance for me
I tried the https://json2csharp.com/ website, but according to the output I need to create a Class for each parent object ("UselessText1", "UselessText2", etc) and they just contain id and biCode. I tried to use it, but (as I would have guessed) it didn't work
It threw this Exception: Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'System.Collections.Generic.List`1[Root]' because the type requires a JSON array (e.g. [1,2,3]) to deserialize correctly.
This is the json:
{
"UselessText1": [],
"UselessText2": [
{
"id": 2,
"biCode": "Something useful"
}
],
"UselessText3": [
{
"id": 3,
"biCode": "Something useful"
},
{
"id": 4,
"biCode": "Something useful"
}
]}
Any idea how to deserialize this? Any Google result I found didn't show a situation like mine. I probably didn't know exactly what to search for
try this
var json=...your json string
var deseializedJson = JsonConvert.DeserializeObject< Dictionary<string,UselessText[]>>(json);
var output = JsonConvert.SerializeObject(deseializedJson);
public class UselessText
{
public int id { get; set; }
public string biCode { get; set; }
}
output
{"UselessText1":[],
"UselessText2":[{"id":2,"biCode":"Something useful"}],
"UselessText3":[{"id":3,"biCode":"Something useful"},{"id":4,"biCode":"Something useful"}]}
Your Json example is a little bit weird. It looks like you have an object that contains multiple properties of the same type.
Something that should work for this exact Json would be:
public class MyModel{
public IEnumerable<MyEntity> UselessText1;
public IEnumerable<MyEntity> UselessText2;
public IEnumerable<MyEntity> UselessText3;
}
public class MyEntity{
public int Id;
public string BiCode;
}

Accessing JSON object from api in C# [duplicate]

This question already has answers here:
Cannot deserialize the current JSON array (e.g. [1,2,3]) into type
(5 answers)
Cannot deserialize the JSON array (e.g. [1,2,3]) into type ' ' because type requires JSON object (e.g. {"name":"value"}) to deserialize correctly
(6 answers)
Closed 2 years ago.
Im doing a Microsoft Cognitive Services project with langauge detection and text translation.
Im receiving Json from the Microsoft Api, and i want to access it as an object.
I've tried a few different things, including JsonConvert.DeserializeObject, and mapping it to my own objects. (Example below)
// Send the request and get response.
HttpResponseMessage response = await client.SendAsync(request).ConfigureAwait(false);
// Read response as a string.
if (response.ReasonPhrase == "OK")
{
//string result = await response.Content.ReadAsStringAsync();
Root myDeserializedClass = JsonConvert.DeserializeObject<Root>(result);
}
// The response:
[{
"detectedLanguage":
{
"language":"fi",
"score":1.0
},
"translations":
[
{"text":"This is a test, I hope it works. The text is in Finnish.","to":"en"},
{"text":"Dette er en test, jeg håber det virker. Teksten er på finsk.","to":"da"}
]
}]
// I found an online tool to generate the appropriate classes for mapping
public class DetectedLanguage
{
[JsonPropertyName("language")]
public string Language { get; set; }
[JsonPropertyName("score")]
public double Score { get; set; }
}
public class Translation
{
[JsonPropertyName("text")]
public string Text { get; set; }
[JsonPropertyName("to")]
public string To { get; set; }
}
public class MyArray
{
[JsonPropertyName("detectedLanguage")]
public DetectedLanguage DetectedLanguage { get; set; }
[JsonPropertyName("translations")]
public List<Translation> Translations { get; set; }
}
public class Root
{
[JsonPropertyName("MyArray")]
public List<MyArray> MyArray { get; set; }
}
// The Error im getting
Newtonsoft.Json.JsonSerializationException: 'Cannot deserialize the current JSON array (e.g. [1,2,3]) into type 'textananlytics_test.Root' because the type requires a JSON object (e.g. {"name":"value"}) to deserialize correctly.
To fix this error either change the JSON to a JSON object (e.g. {"name":"value"}) or change the deserialized type to an array or a type that implements a collection interface (e.g. ICollection, IList) like List<T> that can be deserialized from a JSON array. JsonArrayAttribute can also be added to the type to force it to deserialize from a JSON array.
Path '', line 1, position 1.'
You can change
Root myDeserializedClass = JsonConvert.DeserializeObject<Root>(result);
to
List<MyArray> list = JsonConvert.DeserializeObject<List<MyArray>>(result);
Root myDeserializedClass=new Root{MyArray=list};
In your case, JSON must has next structure:
{
"MyArray": [
{ /* MyArray jobject */ },
{ /* MyArray jobject */ },
...
]
}
So Yiyi You wrote correct processing

c# Deserialize unlabelled JSON array

I am attempting to deserialize a piece of JSON with a specific structure like so:
{
"label1": "value1",
"label2": [
[
[
"concept_id_1",
"concept_1"
],
score_1
],
[
[
"concept_id_2",
"concept_2"
],
score_2
],
……
],
"label3": "value3",
"label4": "value4"
}
For what it's worth, the scores are floats and everything else is a string. The number of returned concepts under "label2" is variable.
I'm attempting to deserialize it using JSON.net. The only content I actually care about is the inside nest of arrays labelled "label2", however the lack of labels inside the arrays is blocking me at every turn.
I've tried a variety of approaches, but the most successful so far seems to be this:
public class Parsed_JSON {
public string label1 { get; set; }
public ICollection<Full_Result> label2 { get; set; }
public string label3 { get; set; }
public string label4 { get; set; }
}
public class Full_Result {
public IList<string> values { get; set; }
public float score { get; set; }
}
Parsed_JSON result = JsonConvert.DeserializeObject<Parsed_JSON>(JSON);
However this is failing with the error:
Cannot deserialize the current JSON array (e.g. [1,2,3]) into type 'JSON_Parsing+Full_Result' because the type requires a JSON object (e.g. {"name":"value"}) to deserialize correctly.
To fix this error either change the JSON to a JSON object (e.g. {"name":"value"}) or change the deserialized type to an array or a type that implements a collection interface (e.g. ICollection, IList) like List that can be deserialized from a JSON array. JsonArrayAttribute can also be added to the type to force it to deserialize from a JSON array.
Ultimately I'll be looking to iterate through the contents of label2 so that I can build a DataTable of them like so:
concept_id_1 concept_1 score_1
concept_id_2 concept_2 score_2
How can I deserialize this JSON?
You can use the custom JsonConverter ObjectToArrayConverter<Full_Result> from this answer to C# JSON.NET - Deserialize response that uses an unusual data structure to deserialize your JSON into your existing typed data model. Modify Full_Result as follows:
[JsonConverter(typeof(ObjectToArrayConverter<Full_Result>))]
public class Full_Result
{
[JsonProperty(Order = 1)]
public IList<string> values { get; set; }
[JsonProperty(Order = 2)]
public float score { get; set; }
}
And you will now be able to deserialize as follows:
Parsed_JSON result = JsonConvert.DeserializeObject<Parsed_JSON>(JSON);
Notes:
ObjectToArrayConverter<T> works by mapping the serializable members of T to an array, where the array sequence is defined by the value of the JsonPropertyAttribute.Order attribute applied to each member. Data contract attributes with DataMemberAttribute.Order set could be used instead, if you prefer.
In your JSON the "score" values are not actually numbers:
score_1
score_2
I am assuming that this is a typo in the question and that these values are in fact well-formed numbers as defined by the JSON standard.
Sample fiddle here.

JSON object deserialize to c# object - OpenTSDB

I am dealing with JSON for the first time and getting data from OpenTSDB. I've created a c# class to deserialize the JSON to but I am getting the error 'Cannot deserialize the current JSON array' as described below.
My c# code to get JSON:
var request = WebRequest.Create("http://localhost:4242/api/query?start=2013/08/21-12:00:00&end=2013/08/22-12:00:00&m=sum:tcollector.collector.lines_sent&o=&yrange=%5B0:%5D&wxh=924x773");
request.ContentType = "application/json; charset=utf-8";
string text;
try
{
var response = (HttpWebResponse) request.GetResponse();
using (var sr = new StreamReader(response.GetResponseStream()))
{
text = sr.ReadToEnd();
}
uxResponse.Text = text;
OpenTSDBResponse myObject = (OpenTSDBResponse)Newtonsoft.Json.JsonConvert.DeserializeObject(text, typeof(OpenTSDBResponse));
var variable = Newtonsoft.Json.JsonConvert.DeserializeObject(text);
//var tester = myObject;
}
catch (Exception ex)
{
uxResponse.Text = GetFullExceptionMessage(ex);
}
The JSON I am receiving from the above code (i.e. the 'text' variable):
[{
"metric":"tcollector.collector.lines_sent",
"tags":
{
"host":"ubuntu1"
},
"aggregateTags":["collector"],
"dps":
{
"1377050434":1271779.0,
"1377050494":1272073.0,
"1377050554":1272502.0,
"1377050614":1273632.0,
"1377050674":1273867.0
}
}]
My c# classes
internal class OpenTSDBResponse
{
[JsonProperty("metric")]
public string Metric { get; set; }
[JsonProperty("tags")]
public Tags Tags { get; set; }
[JsonProperty("aggregateTags")]
public string[] AggregateTags { get; set; }
[JsonProperty("dps")]
public List<TimeValue> TimeValues { get; set; }
}
internal class Tags
{
[JsonProperty("host")]
public string Host { get; set; }
}
internal class TimeValue
{
[JsonProperty("Time")]
public double Time { get; set; }
[JsonProperty("Value")]
public double Value { get; set; }
}
The error when deserializing object:
Cannot deserialize the current JSON array (e.g. [1,2,3]) into type
'MyNamespace.OpenTSDBResponse' because the type requires a JSON
object (e.g. {"name":"value"}) to deserialize correctly.To fix this
error either change the JSON to a JSON object (e.g. {"name":"value"})
or change the deserialized type to an array or a type that implements
a collection interface (e.g. ICollection, IList) like List that can
be deserialized from a JSON array. JsonArrayAttribute can also be
added to the type to force it to deserialize from a JSON array.Path
'', line 1, position 1.
Additional Information
I used the codeproject deserialize JSON project to create my basic classes, but it created a new c# property for each '"1377050434":1271779.0,' so I updated to use my TimeValue class. http://www.codeproject.com/Tips/79435/Deserialize-JSON-with-C
Question:
How do I get this into an appropriate c# class structure?
Additional Information in response to users comments below:
bjaminn's comment:
I believe the JSON you are receiving is an array. The exception is trying to say you are converting an object[] to OpenTSDBResponse when you really want OpenTSDBResponse[]. Another way to debug this would be to look at the variable variable and see what type it is in the debugger. Of course the line that throws the exception would need to be commented out.
Outcome: I modified the deserialize like this
OpenTSDBResponse[] myObject = (OpenTSDBResponse[])Newtonsoft.Json.JsonConvert.DeserializeObject(text, typeof(OpenTSDBResponse[]));
but received the following error when I ran it:
Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'System.Collections.Generic.List`1[MyNamespace.OpenTSDBResponseJsonTypes.TimeValue]' because the type requires a JSON array (e.g. [1,2,3]) to deserialize correctly.To fix this error either change the JSON to a JSON array (e.g. [1,2,3]) or change the deserialized type so that it is a normal .NET type (e.g. not a primitive type like integer, not a collection type like an array or List) that can be deserialized from a JSON object. JsonObjectAttribute can also be added to the type to force it to deserialize from a JSON object.Path '[0].dps.1377050434', line 1, position 121.
Additional Notes on working solution for other new to JSON
I have added another property to my class for the Dictionary as it's really "unix-time-stamp-data","Value". This allows me to work in c# with datetime/values. There may be a better way for casting but this works an doesn't cause any noticeable performance issues for my scenario.
[JsonProperty("dps")]
public Dictionary<string, double> TimeValues { get; set; }
public List<TimeValue> DataPoints
{
get
{
List<TimeValue> times = new List<TimeValue>();
DateTime dtDateTime = new DateTime(1970, 1, 1, 0, 0, 0, 0);
foreach (var item in TimeValues)
{
times.Add(new TimeValue
{
Time = dtDateTime.AddSeconds(double.Parse(item.Key)).ToLocalTime(),
Value = item.Value
});
}
return times;
}
}
I believe the JSON you are receiving is an array. The exception is trying to say you are converting an object[] to OpenTSDBResponse when you really want OpenTSDBResponse[].
Another way to debug this would be to look at the variable variable and see what type it is in the debugger. Of course the line that throws the exception would need to be commented out.
Tackling new error
It looks like DPS is not a proper JSON array. You could parse it to a dictionary since it looks like the keys will be different in each JSON call.
JSON convert dictionary to a list of key value pairs
New class:
internal class OpenTSDBResponse
{
[JsonProperty("metric")]
public string Metric { get; set; }
[JsonProperty("tags")]
public Tags Tags { get; set; }
[JsonProperty("aggregateTags")]
public string[] AggregateTags { get; set; }
[JsonProperty("dps")]
public Dictionary<string,double> TimeValues { get; set; }
}
You can such so modify your Json Data and your C# Code,for example
[{
"metric":"tcollector.collector.lines_sent",
"tags":
{
"host":"ubuntu1"
},
"aggregateTags":["collector"],
"dps":
[
{"Time":"1377050434","Value":1271779.0},
{"Time":"1377050554","Value":1272502.0}
]
}]
c# Code:
You provide the data is an Array,so when you deserialize the string,you must such so use generic format of deserializeobject
object obj=Newtonsoft.Json.JsonConvert.DeserializeObject<List<OpenTSDBResponse>>(json.ToString());

Deserializing JSON using C#

Finding some difficulty in sourcing information in trying to deserialize JSON in C#.
I have results from Google custom search returned in JSON format. I just want to check through my steps and establish the order in trying to deserialize it. Is this right?
I need to create classes to match
the JSON format. Kind of like a
creating schema file.
Use the JavaScriptSerializer() class and
deserialize method to extract the
relevant bits.
One of the issues I think I am going to run into is that I don't require all of the data returned but only the html links. How can I achieve that?
UPDATE
I have updated my question with the following JSON snippet and C# code. I want to output the string 'links' to console but it doesn't seem to be working. I think I am defining my classes wrongly?
JSON from Google Custom Search
handleResponse({
"kind": "customsearch#search",
"url": {
"type": "application/json",
"template": "https://www.googleapis.com/customsearch/v1?q\u003d{searchTerms}&num\u003d{count?}&start\u003d{startIndex?}&hr\u003d{language?}&safe\u003d{safe?}&cx\u003d{cx?}&cref\u003d{cref?}&sort\u003d{sort?}&alt\u003djson"
},
"queries": {
"nextPage": [
{
"title": "Google Custom Search - lectures",
"totalResults": 9590000,
"searchTerms": "lectures",
"count": 1,
"startIndex": 2,
"inputEncoding": "utf8",
"outputEncoding": "utf8",
"cx": "017576662512468239146:omuauf_lfve"
}
],
"request": [
{
"title": "Google Custom Search - lectures",
"totalResults": 9590000,
"searchTerms": "lectures",
"count": 1,
"startIndex": 1,
"inputEncoding": "utf8",
"outputEncoding": "utf8",
"cx": "017576662512468239146:omuauf_lfve"
}
]
},
"context": {
"title": "Curriculum",
"facets": [
[
{
"label": "lectures",
"anchor": "Lectures"
}
],
[
{
"label": "assignments",
"anchor": "Assignments"
}
],
[
{
"label": "reference",
"anchor": "Reference"
}
]
]
},
"items": [
{
"kind": "customsearch#result",
"title": "EE364a: Lecture Videos",
"htmlTitle": "EE364a: \u003cb\u003eLecture\u003c/b\u003e Videos",
"link": "http://www.stanford.edu/class/ee364a/videos.html",
"displayLink": "www.stanford.edu",
"snippet": "Apr 7, 2010 ... Course materials. Lecture slides · Lecture videos (2008) · Review sessions. Assignments. Homework · Reading. Exams. Final exam ...",
"htmlSnippet": "Apr 7, 2010 \u003cb\u003e...\u003c/b\u003e Course materials. \u003cb\u003eLecture\u003c/b\u003e slides · \u003cb\u003eLecture\u003c/b\u003e videos (2008) · Review sessions. \u003cbr\u003e Assignments. Homework · Reading. Exams. Final exam \u003cb\u003e...\u003c/b\u003e",
"cacheid": "TxVqFzFZLOsJ"
}
]
}
);
C# Snippet
public class GoogleSearchResults
{
public string link { get; set; }
}
public class Program
{
static void Main(string[] args)
{
//input search term
Console.WriteLine("What is your search query?:");
string searchTerm = Console.ReadLine();
//concantenate the strings using + symbol to make it URL friendly for google
string searchTermFormat = searchTerm.Replace(" ", "+");
//create a new instance of Webclient and use DownloadString method from the Webclient class to extract download html
WebClient client = new WebClient();
string Json = client.DownloadString("https://www.googleapis.com/customsearch/v1?key=My Key&cx=My CX&q=" + searchTermFormat);
//create a new instance of JavaScriptSerializer and deserialise the desired content
JavaScriptSerializer js = new JavaScriptSerializer();
GoogleSearchResults results = js.Deserialize<GoogleSearchResults>(Json);
Console.WriteLine(results);
//Console.WriteLine(htmlDoc);
Console.ReadLine();
}
}
Thanks
I use your #2 approach: deserialize with the JavaScriptSerializer.
This is what I do to deserialize a response from Facebook:
// get the id for the uploaded photo
var jss = new JavaScriptSerializer();
var resource = jss.Deserialize<Facebook.Data.Resource>(responseText);
....where Facebook.Data.Resource is defined like this:
namespace Facebook.Data
{
public class Resource
{
public string id { get; set; }
}
}
The responseText that I am deserializing from looks like this:
{"id":"10150111918987952",
"from":{"name":"Someone",
"id":"782272221"},
"name":"uploaded from Cropper. (at 12\/15\/2010 7:06:41 AM)",
"picture":"http:\/\/photos-f.ak.fbcdn.net\/hphotos-ak-snc4\/hs817.snc4\/69790_101501113333332_782377951_7551951_8193638_s.jpg",
...
But since I have only one property defined in the Resource class, I only deserialize that. Define the fields in your class that you want to deserialize.
It works to use inheritance, of course. You can define your data classes like this:
namespace Facebook.Data
{
public class Resource
{
public string id { get; set; }
}
public class Person : Resource
{
public string name { get; set; }
}
}
...and then you can deserialize a Person object.
EDIT
Ok, given the sample json you provided in the updated question, here's how I wrote the classes to hold the response:
public class GoogleSearchItem
{
public string kind { get; set; }
public string title { get; set; }
public string link { get; set; }
public string displayLink { get; set; }
// and so on... add more properties here if you want
// to deserialize them
}
public class SourceUrl
{
public string type { get; set; }
public string template { get; set; }
}
public class GoogleSearchResults
{
public string kind { get; set; }
public SourceUrl url { get; set; }
public GoogleSearchItem[] items { get; set; }
// and so on... add more properties here if you want to
// deserialize them
}
And here's the C# code to deserialize:
// create a new instance of JavaScriptSerializer
JavaScriptSerializer s1 = new JavaScriptSerializer();
// deserialise the received response
GoogleSearchResults results = s1.Deserialize<GoogleSearchResults>(json);
Console.WriteLine(s1.Serialize(results));
Some comments:
The toplevel class to hold the search result is called GoogleSearchResults.
The first property in the GoogleSearchResults class is kind, corresponding to the first named property in the json object. You had link which isn't going to work, because link is not the name of a top-level property in that json object. There are properties lower in the hierarchy of your json named "link" but JavaScriptSerializer won't pull out those lower level things into the higher level.
The next property in my GoogleSearchResults class is of type SourceUrl. This is because the url property in the json is not a simple string - it is a json object with two properties, each with a string value. So SourceUrl as a class in C# gets two string properties, each with the appropriate name to deserialize one of those named properties.
the next property in the GoogleSearchResults class is called "items" so that it can deserialize the items dictionary from your json. Now items, as the name suggests, is an array in the json, as denoted by the square bracket around its value. This means there can be more than one item, although in your case there is just one item. So this property in C# must be an array (or collection). Each item in the json result is not a simple string, so, once again, as we did with SourceUrl, we need to define a holder class to deserialize the item object: GoogleSearchItem. This class has a bunch of simple string properties. The properties in the C# class could also be of type int or some other type, if that's what the json requires.
finally, when printing out the result, if you just call Console.WriteLine(result) you will see the result of the ToString() method that is implicitly invoked by Console.WriteLine. This will merely print the name of the type, in this case is "GoogleSearchResults", which is not what you want, I think. In order to see what's in the object, you need to serialize it, as I've shown. In the output of that, you will see only the values of things you deserialized. Using the classes I provided, the result will have less information than the original, because I didn't provide properties in the C# class corresponding to some of the json properties, so those weren't deserialized.
You could take a look at Json.NET and its LINQ support to create and query JSON. By crafting a nice LINQ query you will get only the stuff you need (you can select, group by, count, min, max, whatever you like).
http://msdn.microsoft.com/en-us/library/bb412170.aspx
http://msdn.microsoft.com/en-us/library/bb410770.aspx
Pull out the property you need after you have converted the JSON representation to a type in your C# app. I don't think there's a way to extract only one property from the JSON representation before you have converted it (though I am not sure).

Categories

Resources