Proper use of "$ref" property in JSON Pagination / Json.net - c#

I'm trying to write a C# class for a RESTful web service which returns some items but uses JSON Pagination. I'm using JSON.net for serialization and deserialization.
I have the following code:
static void RefTest()
{
PageTest PageToTest = new PageTest();
PageToTest.next = new PageTest.PageLink();
PageToTest.next.reference = "https://www.example.com/RESTQuery/?page=1";
PageToTest.items = new List<string>();
PageToTest.items.Add("Car");
PageToTest.items.Add("Truck");
PageToTest.items.Add("Plane");
string JSON = JsonConvert.SerializeObject(PageToTest);
PageTest DeserializedPageTest = JsonConvert.DeserializeObject<PageTest>(JSON);
}
While the class looks as follows:
public class PageTest
{
public PageLink next;
public List<string> items;
public class PageLink
{
[JsonProperty("$ref")]
public string reference;
}
}
When I serialize that to a string, I get the expected result:
{
"next": {
"$ref":"https://www.example.com/RESTQuery/?page=1"
},
"items":
[
"Car",
"Truck",
"Plane"
]
}
Until now, everything works fine, it looks like json.net doesn't do anything special with the $ref property when serializing to a string.
Upon parsing of the string, however, json.net recognized the property named "$ref" as an reference to some id (which it can't find) and does NOT put it back on the deserialized object.
This is to be expected, but the web service is handing out pagination that way (With "next", "previous" and "last" attributes).
Basically, since it's impractical (I guess?) for the server to just give me all the values I will have to live with the pagination. So when I want to get all the items, I will have to deserialize the JSON response, check for additional items (indicated when there is a "next" property) and do the process again, until I have retrieved all the data I want.
Do you have any ideas on how I could handle that?
Thanks!

Related

Mapping a json array (without keys) to a class object C#

I need some help to map a json array without keys. For examples:
[
"value1",
234,
3034,
"data",
[
"some value",
null,
2020
],
]
I created a class based on these values received, but i'm not able to map to a class object. I just could access this data using a dynamic variable, like:
dynamic object = DataFromJson();
var firstvalues = object[0]; // "value1"
My json is too big with many arrays inside, so accessing with indexes is a big big process.
Does someones know how to access this kind of data and map to a class?
I received a lot of feedbacks regarding that (thanks!), so here is the conclusions:
as a JSON array, this kind of information is not correct to map to a class object. This is not the functional proposal.
JArray can be really helpful to interate between all the information, and then take it to a class. If needed, you could manually create a specific method to include each value in your class object.
Another option is to use a dynamic object, accessing as indexes.
Make sure that this information can not be received in another way, as a JSON with key and values, and map to a class.
If there is some useful update about that, I will edit here.
Well there is one option available, using Cinchoo ETL - an open source library to load such data to objects out of the box.
Define object as below
[ChoSourceType(typeof(object[]))]
[ChoTypeConverter(typeof(ChoArrayToObjectConverter))]
public class foo
{
[ChoArrayIndex(0)]
public long prop1 { get; set; }
[ChoArrayIndex(1)]
public double prop2 { get; set; }
}
Then parse the JSON as below
string json = #"
[
[
1618170480000,
""59594.60000000"",
""59625.00000000"",
""59557.13000000""
],
[
1618170540000
]
]";
using (var r = ChoJSONReader<foo>.LoadText(json))
{
foreach (var rec in r)
Console.WriteLine(ChoUtility.Dump(rec));
}

Dynamically reading .json file with multiple objects

An application is providing me a .json file that contains an unknown amount of objects. On top of that, the objects content also changes quite a bit.
Here is an example of a json file:
{
"AVRunning":{
"Description":"Symantec Antivirus Service",
"Status":true
},
"CorrectOU":{
"Description":"Server in Correct OU",
"Status":true
},
"RoleAdGroups":{
"Result":true,
"Group":"Server-Admin"
},
"DefaultAdGroups":[
{
"Result":true,
"Group":"Domain Admins"
},
{
"Result":true,
"Group":"Server-Admins"
},
{
"Result":true,
"Group":"SERVERNAME-ADMINS"
}
]
}
This is just an example of a json file. It could contain many more objects, as well as an unknown amount of content within it.
My question to you all, is how do I serialize this into a JSON object in c# once I read the content in as one gigantic string?
Also, I may be overcomplicationg the process here. Once I read this in, I want to pass the data to my view. If this can't be done in c#, would it be easier to do it in Jquery? Or should it be done there in the first place? The file that I am reading in is a local file, so I can't access the data directly in jquery.
I know JSON.NET is a possible tool, but from what I have read, I am not sure how to create the objects dynamically.
You have to define data types into which you want to deserialize json string:
public class ObjectWithDescriptionAndStatus
{
public string Description;
public string Status;
}
public class ObjectWithResultAndGroup
{
public bool Result;
public string Group;
}
public class MyObject
{
public ObjectWithDescriptionAndStatus AVRunning;
public ObjectWithDescriptionAndStatus CorrectOU;
public ObjectWithResultAndGroup RoleAdGroups;
public IList<ObjectWithResultAndGroup> DefaultAdGroups;
}
This is the simplest example, with all fields marked as public and named exactly as keys in the json (they can have arbitrary names but in that case you have to decorate each field with DataMember attribute and set its Name property to the actual name of the key).
Having data types defined, deserialization is a one-liner:
MyObject myObject = Newtonsoft.Json.JsonConvert.DeserializeObject<MyObject>(json);
jQuery makes it pretty simple (just jQuery.parseJSON(<your string>);).
I imagine JSON.NET has a parse function as well, but I haven't used that library, so I couldn't say for sure!

handling JSON response that is not the same each time

i am working with json.net to deserialize json -> C# objects.
it works great in most cases but there are times where rather than getting an array i get object.
so my class (generated using http://json2csharp.com/ and modified to add property).
where i more than 1 arrival methods (such as pick up or ship) it works fine.
public class ArrivalMethods
{
[JsonProperty(PropertyName = "ArrivalMethod")]
public List<string> ArrivalMethod { get; set; }
}
but it breaks where there only 1 arrival method in the json response from my service because i believe json.net is expecting an object like below.
public class ArrivalMethods
{
[JsonProperty(PropertyName = "ArrivalMethod")]
public string ArrivalMethod { get; set; }
}
i am new to json etc. so i am not sure what i am doing wrong. but this throws exception.
Error converting value \"Ship\" to type 'System.Collections.Generic.List`1[System.String]'.
Path 'ProductDetail.SoftHardProductDetails.Fulfillment.
ArrivalMethods.ArrivalMethod', line 1, position 113."
here is where it breaks:
"ArrivalMethods":{
"ArrivalMethod":"Ship"
},
Your JSON should contain [] where there is a list. And if I'm understanding your answer correct, the first class example is the way you want to end up? If so you need the JSON on the bottom to mark ArrivalMethod as a list, wich its not now.
"ArrivalMethods":
{
"ArrivalMethod":["Ship"]
},
To be honest i get a little confused when there is a list with no plural ending.
If you can change the response format that would be the best. It really looks like XML semantics converted to JSON...
If you can't change the response format, you can try to use JsonConverterAttribute to create custom converters for those properties.

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).

Deserialize Json in C#

{
"person": "david",
"images": {
"usable_sizes": [
[
[150,
41
],
"image1.png"
],
[
[220,
61
],
"image2.png"
],
[
[220,
61
],
"image3.png"
]
],
"uploader": null
}
}
I am using a JavaScriptSerializer in C# to parse some JSON. The above is the string I get back from the webrequest. Calling Deserialize on the string puts the Person in the right place in the object, but I don't know what type to use for the array nested under "images." I am at a complete loss.
here is relevant code:
TopLevelObject topObj = new JavaScriptSerialize().Deserialize<TopLevelObj>(jsonStream);
public TopLevelObject
{
public string person;
public imgStruct images;
}
public class imgStructure
{
public List<string[,][]> available_sizes;
}
but that's not taking. I have tried a variety of other forms for the class, but can't get the deserialize to put the data in without proper keys. I have no ability to change the inbound JSON as I am consuming it from a third party.
This is what I believe your classes should look like:
public class RootObj
{
public string person;
public ImageDataObj images;
}
public class ImageDataObj
{
public object uploader;
public List<List<object>> usable_sizes
}
As you can see the inner list is a list of objects. That is because the list has items of different types: one is a list (numerical data [150, 41]) and the other is a string (image name). I do not think you can strongly type the inner list for that reason.
Maybe it is worth examining on runtime (using the VS debugging tools) the structure of the objects inside the inner list and that could give you an indication of how to map it.
How the usable_images property type does look like if you take it to its detail is:
public List<List<Dictionary<List<int>,string>>> usable_sizes
but that is just a guess.
I would advise going to JSON.org and using one of their pre-built solutions.

Categories

Resources