Deserialize complex JSON result - c#

I am automating an API. I would like to deserialize the JSON response I get but I can't figure out how to do it. This is the JSON I get as response:
{
"class": [
"Paged-Collection",
"Products"
],
"properties": {
"defaultPageSize": 10,
"skip": null,
"top": 10,
"totalRows": 2
},
"entities": [
{
"class": [
"Product"
],
"rel": [],
"properties": {
"Supplier": "Supplier1",
"Currency": "USD",
"ProductCode": "SomeCode1",
"price": 2
}
},
{
"class": [
"Product"
],
"rel": [],
"properties": {
"Supplier": "Supplier2",
"Currency": "USD",
"ProductCode": "SomeCode2",
"price": 73
}
},
],
"links": [
{
"rel": [
"self"
],
"href": "http://localhost...",
"method": "GET",
"title": null,
"type": null
}
]
}
from what I've seen in other examples the deserialization boils down to figuring out the exact data strucutre (Not sure though if this what it is) however I made several attempts (not just rushing to SO for answer), so this is my last try:
The structure as I see it:
public class Data
{
List<string> #class { get; set; }
Dictionary<string, MetaData> properties { get; set; }
List<entities> entities { get; set; }
List<LinkFeed> links { get; set; }
}
public class MetaData
{
public int defaultPageSize { get; set; }
public string skip { get; set; }
public int top { get; set; }
public int totalRows { get; set; }
}
public class entities
{
public List<string> #class { get; set; }
public List<string> rel { get; set; }
public properties property { get; set; }
}
public class properties
{
public string Supplier { get; set; }
public string Currency { get; set; }
public string ProductCode { get; set; }
public string price { get; set; }
public double Price { get; set; }
}
public class LinkFeed
{
public List<string> rel { get; set; }
public string href { get; set; }
public string method { get; set; }
public string title { get; set; }
public string type { get; set; }
}
And the actual deserialization using Newtonsoft.Json :
var result = JsonConvert.DeserializeObject<Data>(data);
And everything is Null..
Actually the data that I'm really interested in is that under "entities" but I don't know if extracting only that will be actually easier or more difficult.

This is your structure:
public class Rootobject {
public string[] _class { get; set; }
public Properties properties { get; set; }
public Entity[] entities { get; set; }
public Link[] links { get; set; }
}
public class Properties {
public int defaultPageSize { get; set; }
public object skip { get; set; }
public int top { get; set; }
public int totalRows { get; set; }
}
public class Entity {
public string[] _class { get; set; }
public object[] rel { get; set; }
public Properties1 properties { get; set; }
}
public class Properties1 {
public string Supplier { get; set; }
public string Currency { get; set; }
public string ProductCode { get; set; }
public int price { get; set; }
}
public class Link {
public string[] rel { get; set; }
public string href { get; set; }
public string method { get; set; }
public object title { get; set; }
public object type { get; set; }
}
So you will do this:
var result = JsonConvert.DeserializeObject<Rootobject>(data);
I would rename the class Properties to another name because it will clash with the Properties class of the assembly. This will give you headaches so just to be safe call it something else.
Also if you do not like some of the property names in the above classes, I mean I do not like it because it does not follow .NET naming conventions, then you can do this:
public class Rootobject {
[JsonProperty( "class" )]
public string[] MyClass { get; set; }
// ... other properties
}
Obviously calling it MyClass is just an example, so give it a name that reflects the business domain.

Related

Why is my inner array not being deserialized?

I have some JSON which I'd like to deserialize into Objects.
I'm trying to do it like this, but I only get some of the JSON deserialized and the vin and vout JSON are either 0s or nulls. What am I doing wrong?
Transaction t = JsonConvert.DeserializeObject<RPCResponse<Transaction>>(json).result;
JSON:
{
"result": {
"hex": "01000000010000000000000000000000000000000000000000000000000000000000000000ffffffff0704ffff001d0104ffffffff0100f2052a0100000043410496b538e853519c726a2c91e61ec11600ae1390813a627c66fb8be7947be63c52da7589379515d4e0a604f8141781e62294721166bf621e73a82cbf2342c858eeac00000000",
"txid": "0e3e2357e806b6cdb1f70b54c3a3a17b6714ee1f0e68bebb44a74b1efd512098",
"hash": "0e3e2357e806b6cdb1f70b54c3a3a17b6714ee1f0e68bebb44a74b1efd512098",
"size": 134,
"version": 1,
"locktime": 0,
"vin": [
{
"coinbase": "04ffff001d0104",
"sequence": 4294967295
}
],
"vout": [
{
"value": 50.00000000,
"n": 0,
"scriptPubKey": {
"asm": "0496b538e853519c726a2c91e61ec11600ae1390813a627c66fb8be7947be63c52da7589379515d4e0a604f8141781e62294721166bf621e73a82cbf2342c858ee OP_CHECKSIG",
"hex": "410496b538e853519c726a2c91e61ec11600ae1390813a627c66fb8be7947be63c52da7589379515d4e0a604f8141781e62294721166bf621e73a82cbf2342c858eeac",
"reqSigs": 1,
"type": "pubkey",
"addresses": [
"12c6DSiU4Rq3P4ZxziKxzrL5LmMBrzjrJX"
]
}
}
],
"blockhash": "00000000839a8e6886ab5951d76f411475428afc90947ee320161bbf18eb6048",
"confirmations": 563356,
"time": 1231469665,
"blocktime": 1231469665
},
"error": null,
"id": "getrawtransaction"
}
The output in the debugger looks like this:
My Classes look like this:
public class RPCResponse<T>
{
public T result { get; set; }
public string error { get; set; }
public string id { get; set; }
}
public class Transaction
{
public string TxId { get; set; }
public long Size { get; set; }
public long Version { get; set; }
public long LockTime { get; set; }
public TransactionInputCoinbase[] Vin { get; set; }
public TransactionOutput[] Vout { get; set; }
public string BlockHash { get; set; }
public long Time { get; set; }
}
public class TransactionInputCoinbase
{
string Coinbase { get; set; }
string Sequence { get; set; }
}
public class TransactionOutput
{
decimal Value { get; set; }
int N { get; set; }
ScriptPubKey ScriptPubKey { get; set; }
}
public class ScriptPubKey
{
string Asm { get; set; }
string Hex { get; set; }
long ReqSigs { get; set; }
string Type { get; set; }
string[] Addresses { get; set; }
}
The lack of public properties on the said objects
For example
public class TransactionInputCoinbase
{
string Coinbase { get; set; } //<--NOT PUBLIC
string Sequence { get; set; } //<--NOT PUBLIC
}
means those properties will never be set when initialized via deserialization.
The same goes for TransactionOutput and ScriptPubKey based on the code originally provided.

Deserializing result of steam storefront api using RestSharp always returns null

I've been trying to use the steam storefront api http://store.steampowered.com/api/.
The problem is that when I feed the result of the api to RestSharp, the data is always null.
So my question is how do I get the Rest call deserialized?
First, this is what you would be expecting when calling appdetails?appids=[appid]
{
"3400": {
"success": true,
"data": {
"type": "game",
"name": "Hammer Heads Deluxe",
"steam_appid": 3400,
"required_age": 0,
"is_free": false,
"detailed_description": "",
"about_the_game": "",
"short_description": "",
"supported_languages": "English",
"header_image": "http:\/\/cdn.akamai.steamstatic.com\/steam\/apps\/3400\/header.jpg?t=1447350883",
"website": null,
"pc_requirements": {
"minimum": "<p><strong>Minimum Requirements:<\/strong> Windows 98\/ME\/2000\/XP, 128 MB RAM, 500MHz or faster, DirectX: 7.0<\/p>"
},
"mac_requirements": [
],
"linux_requirements": [
],
"developers": [
"PopCap Games, Inc."
],
"publishers": [
"PopCap Games, Inc."
],
"demos": [
{
"appid": 3402,
"description": ""
}
],
"price_overview": {
"currency": "USD",
"initial": 499,
"final": 499,
"discount_percent": 0
},
"packages": [
135
],
"package_groups": [
{
"name": "default",
"title": "Buy Hammer Heads Deluxe",
"description": "",
"selection_text": "Select a purchase option",
"save_text": "",
"display_type": 0,
"is_recurring_subscription": "false",
"subs": [
{
"packageid": 135,
"percent_savings_text": "",
"percent_savings": 0,
"option_text": "Hammer Heads Deluxe - $4.99",
"option_description": "",
"can_get_free_license": "0",
"is_free_license": false,
"price_in_cents_with_discount": 499
}
]
}
],
"platforms": {
"windows": true,
"mac": false,
"linux": false
},
"categories": [
{
"id": 2,
"description": "Single-player"
}
],
"genres": [
{
"id": "4",
"description": "Casual"
}
],
"screenshots": [
{
"id": 0,
"path_thumbnail": "http:\/\/cdn.akamai.steamstatic.com\/steam\/apps\/3400\/0000000564.600x338.jpg?t=1447350883",
"path_full": "http:\/\/cdn.akamai.steamstatic.com\/steam\/apps\/3400\/0000000564.1920x1080.jpg?t=1447350883"
},
{
"id": 1,
"path_thumbnail": "http:\/\/cdn.akamai.steamstatic.com\/steam\/apps\/3400\/0000000565.600x338.jpg?t=1447350883",
"path_full": "http:\/\/cdn.akamai.steamstatic.com\/steam\/apps\/3400\/0000000565.1920x1080.jpg?t=1447350883"
},
{
"id": 2,
"path_thumbnail": "http:\/\/cdn.akamai.steamstatic.com\/steam\/apps\/3400\/0000000566.600x338.jpg?t=1447350883",
"path_full": "http:\/\/cdn.akamai.steamstatic.com\/steam\/apps\/3400\/0000000566.1920x1080.jpg?t=1447350883"
},
{
"id": 3,
"path_thumbnail": "http:\/\/cdn.akamai.steamstatic.com\/steam\/apps\/3400\/0000000567.600x338.jpg?t=1447350883",
"path_full": "http:\/\/cdn.akamai.steamstatic.com\/steam\/apps\/3400\/0000000567.1920x1080.jpg?t=1447350883"
},
{
"id": 4,
"path_thumbnail": "http:\/\/cdn.akamai.steamstatic.com\/steam\/apps\/3400\/0000000568.600x338.jpg?t=1447350883",
"path_full": "http:\/\/cdn.akamai.steamstatic.com\/steam\/apps\/3400\/0000000568.1920x1080.jpg?t=1447350883"
}
],
"release_date": {
"coming_soon": false,
"date": "Aug 30, 2006"
},
"support_info": {
"url": "",
"email": ""
},
"background": "http:\/\/cdn.akamai.steamstatic.com\/steam\/apps\/3400\/page_bg_generated_v6b.jpg?t=1447350883"
}
}
}
That is the json block returned for accessing appid 3400. Note how it's appid is located inside of file itself.
In order to generate classes for this, I made a formatted version of that based off the unofficial storefront api documentation. Then I used that to generate the container classes shown below.
public class Appid
{
public bool success { get; set; }
public Data data { get; set; }
}
public class Data
{
public string type { get; set; }
public string name { get; set; }
public int steam_appid { get; set; }
public int required_age { get; set; }
public int[] dlc { get; set; }
public string detailed_description { get; set; }
public string about_the_game { get; set; }
public Fullgame fullgame { get; set; }
public string supported_languages { get; set; }
public string header_image { get; set; }
public string website { get; set; }
public Pc_Requirements pc_requirements { get; set; }
public Mac_Requirements mac_requirements { get; set; }
public Linux_Requirements linux_requirements { get; set; }
public string[] developers { get; set; }
public Demos demos { get; set; }
public Price_Overview price_overview { get; set; }
public int[] packages { get; set; }
public Package_Groups[] package_groups { get; set; }
public Platforms platforms { get; set; }
public Metacritic metacritic { get; set; }
public Category[] categories { get; set; }
public Screenshot[] screenshots { get; set; }
public Movie[] movies { get; set; }
public Recommendations recommendations { get; set; }
public Achievements achievements { get; set; }
public Release_Date release_date { get; set; }
}
public class Fullgame
{
public int appid { get; set; }
public string name { get; set; }
}
public class Pc_Requirements
{
public string minimum { get; set; }
public string recommended { get; set; }
}
public class Mac_Requirements
{
public string minimum { get; set; }
public string recommended { get; set; }
}
public class Linux_Requirements
{
public string minimum { get; set; }
public string recommended { get; set; }
}
public class Demos
{
public int appid { get; set; }
public string description { get; set; }
}
public class Price_Overview
{
public string currency { get; set; }
public int initial { get; set; }
public int final { get; set; }
public int discount_precent { get; set; }
}
public class Platforms
{
public bool windows { get; set; }
public bool mac { get; set; }
public bool linux { get; set; }
}
public class Metacritic
{
public int score { get; set; }
public string url { get; set; }
}
public class Recommendations
{
public int total { get; set; }
}
public class Achievements
{
public int total { get; set; }
public Highlighted[] highlighted { get; set; }
}
public class Highlighted
{
public string name { get; set; }
public string path { get; set; }
}
public class Release_Date
{
public bool coming_soon { get; set; }
public string date { get; set; }
}
public class Package_Groups
{
public string name { get; set; }
public string title { get; set; }
public string description { get; set; }
public string selection_text { get; set; }
public string save_text { get; set; }
public int display_type { get; set; }
public string is_recurring_subscription { get; set; }
public Sub[] subs { get; set; }
}
public class Sub
{
public int packageid { get; set; }
public string percent_savings_text { get; set; }
public int percent_savings { get; set; }
public string option_text { get; set; }
public string option_description { get; set; }
}
public class Category
{
public int id { get; set; }
public string description { get; set; }
}
public class Screenshot
{
public int id { get; set; }
public string path_thumbnail { get; set; }
public string path_full { get; set; }
}
public class Movie
{
public int id { get; set; }
public string name { get; set; }
public string thumbnail { get; set; }
public Webm webm { get; set; }
}
public class Webm
{
public string _480 { get; set; }
public string max { get; set; }
}
Now onto the code, the function takes in a long and should return a Data object. However, instead of deserializing, it returns a null. I have checked to make sure the json is actually getting to the code. It is coming in fine.
//Steam rest stuff always returns with one object.
//This it to keep me writing 10 classes that do essentially the same thing
//And before you ask, yes, I have tried doing this without this class.
//Still didn't work.
public class RootObject<T>
{
public T response { get; set; }
}
public static Data GetGameData(long appid)
{
//Only thing different about this function except for the fact that it's also static
RestClient storeClient = new RestClient("http://store.steampowered.com/api/");
RestRequest restReq = new RestRequest("appdetails/", Method.GET);
restReq.AddParameter("appids", appid);
restReq.RequestFormat = DataFormat.Json;
restReq.OnBeforeDeserialization = resp =>
{
//This was done as an attempt to fix it.
//It makes the appid's in the content into {"appId":{
//EX: {"400":{ -> {"appId":{
resp.Content = Regex.Replace(resp.Content, "{\"\\d+\":{", "{\"appId\":{");
};
IRestResponse<RootObject<Appid>> response = storeClient.Execute<RootObject<Appid>>(restReq);
Appid trueRes = response.Data.response;
return trueRes.data;
}
Quick note, the function was altered but only to work in a static context.
The edit was noted in code.
Things I've tried
Explicitly specifying that the file is a json file.
Changing the appid class names to a common class name.
Using Json.Net as the deserializer.
Removing the "success": true item.
Using [DeserializeAs(Name = "appId")] in the appid class.
Using a non generic version of RootObject
Tried using Dictionary<string, Appid> instead of RootObject<Appid>
Edit:
For some extra information I did some testing on the methods.
I remade the object carrying classes using appid 4000 and put that into json to sharp converter. Then I wrote a small bit of code that tested if the data was null.
static void TestIfFetched(long appid)
{
Console.Write($"{appid}\t:");
Data data = GetGameData(appid);
if (data == null)
{
Console.WriteLine("Failed to get data.");
}
else
{
Console.WriteLine("Data fetched successfully.");
}
}
The result was this when testing 4000 and 400.
4000 :Failed to get data.
400 :Failed to get data.
So it seems even using data specifically designed for that game, it still fails to parse.
ServicePointManager.Expect100Continue = true;
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
RestClient storeClient = new RestClient("https://store.steampowered.com/api/");
RestRequest restReq = new RestRequest("appdetails/", Method.GET);
restReq.AddParameter("appids", appid);
restReq.RequestFormat = DataFormat.Json;
var response = storeClient.Execute<dynamic>(restReq);
JObject jObject = JObject.Parse(response.Content);
var details = jObject[appid].Value<JObject>().ToObject<Appid>();
The above worked for me. The JObject allows you to pass the appid as a parameter and then parse your object from there.

using DataContractJsonSerializer knowntype

I am trying to deserealize json:
Classes is generated by json2sharp
But it throws exception:
Element ": Data" contains data from a data type that maps to the name
"http://schemas.datacontract.org/2004/07/TMSoft.CryptoDoc.Gu45:Gu45".
The deserializer is no information of any type, are matched with the
same name. Use DataContractResolver or add type that matches "Gu45",
the list of known types, such as using KnownTypeAttribute attribute or
by adding it to the list of known types passed to
DataContractSerializer.
How to fix it?
namespace json
{
class Program
{
static void Main(string[] args)
{
string json = System.IO.File.ReadAllText(#"D:\Json.txt");
DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(RootObject));
var account = serializer.ReadObject(new MemoryStream(Encoding.UTF8.GetBytes(json)));
}
}
public class Branch
{
public string Code { get; set; }
public string Name { get; set; }
}
public class Direction
{
public object Code { get; set; }
public string Name { get; set; }
}
public class Org
{
public string Code { get; set; }
public string Name { get; set; }
}
public class Wagon
{
public string Comment { get; set; }
public object NoteTimeString { get; set; }
public string Number { get; set; }
public string OwnerCode { get; set; }
public string StateString { get; set; }
}
public class Data
{
public string __type { get; set; }
public Branch Branch { get; set; }
public string DeliveryTimeString { get; set; }
public Direction Direction { get; set; }
public string EsrCode { get; set; }
public int GU45Type { get; set; }
public object ManeuverTime { get; set; }
public string Number { get; set; }
public Org Org { get; set; }
public object ParkName { get; set; }
public object ParkNum { get; set; }
public string RailwayName { get; set; }
public string RegistrationDateTimeString { get; set; }
public object Span { get; set; }
public string StationName { get; set; }
public string TakingTimeString { get; set; }
public object TrackNum { get; set; }
public object TrackNumAddition { get; set; }
public object WagonNote { get; set; }
public List<Wagon> Wagons { get; set; }
}
public class Body
{
public Data Data { get; set; }
public List<int> Signature { get; set; }
}
public class RootObject
{
public Body Body { get; set; }
public int Revision { get; set; }
public int State { get; set; }
public string StateDate { get; set; }
public string WosId { get; set; }
}
}
Json.txt:
{
"Body": {
"Data": {
"__type": "Gu45:#TMSoft.CryptoDoc.Gu45",
"Branch": {
"Code": "9898",
"Name": "Place"
},
"DeliveryTimeString": "07.03.2014 10:00:00",
"Direction": {
"Code": null,
"Name": "Test"
},
"EsrCode": "320007",
"GU45Type": 2,
"ManeuverTime": null,
"Number": "1",
"Org": {
"Code": "1860",
"Name": "Test"
},
"ParkName": null,
"ParkNum": null,
"RailwayName": "Test",
"RegistrationDateTimeString": "07.03.2014",
"Span": null,
"StationName": "Test",
"TakingTimeString": "07.03.2014 12:00:00",
"TrackNum": null,
"TrackNumAddition": null,
"WagonNote": null,
"Wagons": [
{
"Comment": "РЕМТ",
"NoteTimeString": null,
"Number": "44916989",
"OwnerCode": "22",
"StateString": "0"
}
]
},
"Signature": [
48,
106
]
},
"Revision": 1966,
"State": 2,
"StateDate": "2014-03-07T12:48:03Z",
"WosId": "15805729"
}
I had similar experience in my previous projects with DataContractJsonSerializer. it seems that "__type" field has a special meaning for DataContractJsonSerializer.
This is one of the main reasons that I use Json.net
it matters only if it's the first property in the object but if it has any other positions it works and exceptions is not thrown. you can try it but changing __type position below the branch property in Json.txt. I don't know if it's the case and you can find a workaround.
I haven't tried this solution but it worth to give it a try:
hope it helps

Deserializing a complex JSON object

I use JavaScript.Serializer.Deserializer to deserialize a complex JSON object, as below:
{
"name": "rule 1",
"enabled": true,
"conditions": [{
"type": "time",
"time": "17:23:10",
"days": "125"
}, {
"type": "sensor",
"uid": "10.2.0.1",
"setpoint1": 12,
"setpoint2": 18,
"criterion": 1
}, {
"type": "sensor",
"uid": "10.3.0.1",
"setpoint1": 12,
"setpoint2": 18,
"criterion": 2
}],
"actions": {
"period": 100,
"single": false,
"act": [{
"type": "on",
"uid": "10.1.0.1"
}, {
"type": "sms",
"message": "Hello World"
}]
}
}
And I want to convert it to some classes, like below:
public class Rule
{
public string name { get; set; }
public bool enabled { get; set; }
public List<Condition> conditions { get; set; }
public List<Action> actions { get; set; }
}
public class Condition
{
public string type { get; set; }
public string uid { get; set; }
public DateTime? time { get; set; }
public string days { get; set; }
public double setpoint1 { get; set; }
public double setpoint2 { get; set; }
public int criterion { get; set; }
}
public class Action
{
public int period { get; set; }
public bool single { get; set; }
public List<Act> act { get; set; }
}
public class Act
{
public string type { get; set; }
public string uid { get; set; }
public string message { get; set; }
}
The deserialization snippet:
json = new JavaScriptSerializer();
Rule rule = (json.Deserialize<Rule>(jsonStr));
If I simplify the Rule class and declare conditions and actions as simple strings, it works fine.
But if I use the classes like above, it throws an exception:
Cannot convert object of type 'System.String' to type 'System.Collections.Generic.List`1[IoTWebApplication.Condition]'
The structure you create does not fit to the JSON you posted.
It should look like
public class Rule
{
public string name { get; set; }
public bool enabled { get; set; }
public Condition[ ] conditions { get; set; }
public Actions actions { get; set; }
}
public class Actions
{
public int period { get; set; }
public bool single { get; set; }
public Act[ ] act { get; set; }
}
public class Act
{
public string type { get; set; }
public string uid { get; set; }
public string message { get; set; }
}
public class Condition
{
public string type { get; set; }
public string time { get; set; }
public string days { get; set; }
public string uid { get; set; }
public int setpoint1 { get; set; }
public int setpoint2 { get; set; }
public int criterion { get; set; }
}
It is (in most cases) very easy in VS to get the classes direct out of the JSON
Copy JSON to clipboard
In VS EDIT/Special Paste/Paste JSON as Classes (the code above was created by this)
The problem was that the inner (nested) json was quoted and therefore was processed as a string. So when I removed the quotes, it worked fine:
json = new JavaScriptSerializer();
Rule rule = (json.Deserialize<Rule>(jsonStr));

Retrieve the values from json string

I have json string. I want to retrieve the contact from json string. Following json contains array of contacts. here is my json string.
{
   "contacts": {
      "contact": [
         {
            "isConnection": false,
            "id": 33554611,
            "fields": [
               {
                  "id": 33554748,
                  "type": "name",
                  "value": {
                     "givenName": "Jhon",
                     "middleName": "",
                     "familyName": "Scot",
                     "prefix": "",
                     "suffix": "",
                     "givenNameSound": "",
                     "familyNameSound": ""
                  },
                  "editedBy": "OWNER",
                  "flags": [],
                  "categories": [],
                  "updated": "2012-12-23T07:40:23Z",
                  "created": "2012-12-23T07:40:23Z",
               },
               {
                  "id": 33554749,
                  "type": "email",
                  "value": "someone#example.com",
                  "editedBy": "OWNER",
                  "flags": [],
                  "categories": [],
                  "updated": "2012-12-23T07:40:23Z",
                  "created": "2012-12-23T07:40:23Z",
               }
            ]
         }
}
}
Here I want to retrieves the values of givenName,familyName,email. How can I retrieve the values of these from json string.
Note: there are array of contact in json. I have posted only one contact from this json.
I tried something like this. But not worked.
JObject json = JObject.Parse(returnStr);
JArray fields = (JArray)json["contacts"]["contact"]["fields"][0];
JArray FValues = (JArray)json["contact"]["fields"]["value"];
I tried this
public class Field
{
public int id { get; set; }
public string type { get; set; }
public object value { get; set; }
public string editedBy { get; set; }
public List<object> flags { get; set; }
public List<object> categories { get; set; }
public string updated { get; set; }
public string created { get; set; }
public string uri { get; set; }
public bool? isConnection { get; set; }
}
public class contact
{
public bool isConnection { get; set; }
public int id { get; set; }
public List<Field> fields { get; set; }
public List<object> categories { get; set; }
public int error { get; set; }
public int restoredId { get; set; }
public string created { get; set; }
public string updated { get; set; }
public string uri { get; set; }
}
public class Contacts
{
public List<contact> contact { get; set; }
public int count { get; set; }
public int start { get; set; }
public int total { get; set; }
public string uri { get; set; }
public bool cache { get; set; }
}
public class RootObject
{
public Contacts contacts { get; set; }
}
and
JavaScriptSerializer serializer1 = new JavaScriptSerializer();
RootObject obje = serializer1.Deserialize<RootObject>(returnStr);
But it is giving me 0 value in obje.
First make sure your Json is in valid format using jsonlint
Then generate class base on it using json2csharp
public class Field
{
public int id { get; set; }
public string type { get; set; }
public object value { get; set; }
public string editedBy { get; set; }
public List<object> flags { get; set; }
public List<object> categories { get; set; }
public string updated { get; set; }
public string created { get; set; }
}
public class Contact
{
public bool isConnection { get; set; }
public int id { get; set; }
public List<Field> fields { get; set; }
}
public class Contacts
{
public List<Contact> contact { get; set; }
}
public class RootObject
{
public Contacts contacts { get; set; }
}
Use Newtonsoft JSON to deserialize your Json into object(s) then you may simply access its properties value.
JsonConvert.DeserializeObject<RootObject>(string json);
Class for json object (generated with http://jsonutils.com/ after correcting some syntax error):
public class Field
{
public int id { get; set; }
public string type { get; set; }
public object value { get; set; }
public string editedBy { get; set; }
public IList<object> flags { get; set; }
public IList<object> categories { get; set; }
public DateTime updated { get; set; }
public DateTime created { get; set; }
}
public class Contact
{
public bool isConnection { get; set; }
public int id { get; set; }
public IList<Field> fields { get; set; }
}
public class Contacts
{
public IList<Contact> contact { get; set; }
}
public class Example
{
public Contacts contacts { get; set; }
}
Deserialization (you will probably need to add a reference to System.Web.Extensions):
System.Web.Script.Serialization.JavaScriptSerializer deSer = new System.Web.Script.Serialization.JavaScriptSerializer();
JSonPrintSettingsToXml.Input.Example deserializedJSON = deSer.Deserialize<JSonPrintSettingsToXml.Input.Example>(yourJSON);
Here is the corrected JSON
{
"contacts": {
"contact": [
{
"isConnection": false,
"id": 33554611,
"fields": [
{
"id": 33554748,
"type": "name",
"value": {
"givenName": "Jhon",
"middleName": "",
"familyName": "Scot",
"prefix": "",
"suffix": "",
"givenNameSound": "",
"familyNameSound": ""
},
"editedBy": "OWNER",
"flags": [],
"categories": [],
"updated": "2012-12-23T07:40:23Z",
"created": "2012-12-23T07:40:23Z"
},
{
"id": 33554749,
"type": "email",
"value": "someone#example.com",
"editedBy": "OWNER",
"flags": [],
"categories": [],
"updated": "2012-12-23T07:40:23Z",
"created": "2012-12-23T07:40:23Z"
}
]
}
]
}
}
You need to go with the following structure:
public class Contact
{
public bool isConnection { get; set; }
public int id { get; set; }
public List<Field> fields { get; set; }
}
public class Field
{
public int id { get; set; }
public string type { get; set; }
public object value { get; set; }
public string editedBy { get; set; }
public string[] flags { get; set; }
public string[] categories { get; set; }
public DateTime updated { get; set; }
public DateTime created { get; set; }
}
public class Name
{
public string givenName { get; set; }
public string middleName { get; set; }
public string familyName { get; set; }
public string prefix { get; set; }
public string suffix { get; set; }
public string givenNameSound { get; set; }
public string familyNameSound { get; set; }
}
And then deserialize it and use LINQ to manipulate fields.
If you want to stick to JObject and not create classes, look at the example provided at http://weblog.west-wind.com/posts/2012/Aug/30/Using-JSONNET-for-dynamic-JSON-parsing
You'll have to enumerate over this
dynamic contacts = (JArray)json["contacts"]
foreach(dynamic contact in contacts.contact) {
// look at the fields...
}
PS. Give it a go, don't have VS on me so cant quite verify the exact syntax
Firstly, I think you'll find that your JSON is not well-formed. You don't need the extra commas after the two "created" dates and there is a right-square bracket missing before the second last curly brace. I recommend you always validate your JSON using this awesome site: http://jsonformatter.curiousconcept.com
Secondly, you are not referencing the array elements correctly. Although I agree with #grundy that you should be creating class definitions for managing JSON and using LINQ, there is absolutely nothing wrong with the Newtonsoft library. You can still persist with your method.
Try this:-
var json = JObject.Parse(returnStr);
var fields = (JArray)json["contacts"]["contact"][0]["fields"];
var givenName = fields[0]["value"]["givenName"];
var familyName = fields[0]["value"]["familyName"];
var email = fields[1]["value"];

Categories

Resources