How can I deserialize nested JSON arrays? - c#

I am trying to deserialize GeoJson so I can break it up and store it in a Db. When I try to deserialize it, the deserialization of coordinates is failing.
I'm using the following class for a geometry:
public class GeoJsonGeometry {
public string type { get; set; }
public string[, ,] coordinates { get; set; }
}
"geometry": { "type": "Polygon", "coordinates": [ [ [ -85.388717, 33.913044 ], [ -85.380885, 33.873508 ], [ -85.379455, 33.866291 ], [ -85.377426, 33.856047 ], [ -85.376403, 33.850656 ], [ -85.364595, 33.788446 ], [ -85.361844, 33.773951 ], [ -85.360491, 33.767958 ], [ -85.357402, 33.750104 ], [ -85.355252, 33.739245 ], [ -85.344054, 33.682684 ], [ -85.342722, 33.675953 ], [ -85.323792, 33.580339 ], [ -85.315340, 33.537646 ], [ -85.314994, 33.535898 ], [ -85.314843, 33.534951 ], [ -85.314091, 33.530218 ], [ -85.313999, 33.529807 ], [ -85.304439, 33.482884 ], [ -85.308211, 33.481579 ], [ -85.309250, 33.483137 ], [ -85.314852, 33.487603 ],...]]]
I've tried double [,,] but it didn't work either.
I'm confused as this looks like it should serialize nicely as it's nested arrays, but it's not. Any help would be appreciated.
I've also tried List<List<List<double>>> and double[][][] and it always fails.

Your Geometry object should be
public class Geometry
{
public string type { get; set; }
public List<List<List<double>>> coordinates { get; set; }
}
EDIT
var obj = JsonConvert.DeserializeObject<RootObject>(json);
public class Geometry
{
public string type { get; set; }
public List<List<List<double>>> coordinates { get; set; }
}
public class RootObject
{
public Geometry geometry { get; set; }
}
json:
{
"geometry": {
"type": "Polygon",
"coordinates": [
[
[
-85.388717,
33.913044
],
[
-85.380885,
33.873508
],
[
-85.379455,
33.866291
],
[
-85.377426,
33.856047
],
[
-85.376403,
33.850656
],
[
-85.364595,
33.788446
],
[
-85.361844,
33.773951
],
[
-85.360491,
33.767958
],
[
-85.357402,
33.750104
],
[
-85.355252,
33.739245
],
[
-85.344054,
33.682684
],
[
-85.342722,
33.675953
],
[
-85.323792,
33.580339
],
[
-85.31534,
33.537646
],
[
-85.314994,
33.535898
],
[
-85.314843,
33.534951
],
[
-85.314091,
33.530218
],
[
-85.313999,
33.529807
],
[
-85.304439,
33.482884
],
[
-85.308211,
33.481579
],
[
-85.30925,
33.483137
],
[
-85.314852,
33.487603
]
]
]
}
}

JSON does not support multi-dimensional arrrays.
That's an array of arrays of arrays: double[][][].

Related

Unable to Deserialize Json downloaded from a URL

I am trying to deserialize the JSON downloaded from the following site downloaded as RawData
Json from the Site
but following error is being thrown
Newtonsoft.Json.JsonSerializationException: 'Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type '' because the type requires a JSON array (e.g. [1,2,3]) to deserialize correctly.
I tried using following methods
Root[] roots= JsonConvert.DeserializeObject<Root[]>(jsonString);
and
var roots = JsonConvert.DeserializeObject<List<Root>>(jsonString);
I used following class
public class Root
{
public List<object> posts { get; set; }
public List<Person> persons { get; set; }
public List<Organization> organizations { get; set; }
public Meta meta { get; set; }
public List<Membership> memberships { get; set; }
public List<Event> events { get; set; }
public List<Area> areas { get; set; }
}
Following is the example JSON
"posts": [
],
"persons": [
{
"birth_date": "1957-08-09",
"contact_details": [
{
"type": "email",
"value": "hariomsingh.rathore#sansad.nic.in"
}
],
"email": "hariomsingh.rathore#sansad.nic.in",
"gender": "male",
"id": "0094ff14-ff6c-440a-96fc-f0bd68068569",
"identifiers": [
{
"identifier": "4655",
"scheme": "everypolitician_legacy"
},
{
"identifier": "hariomsinghrathore",
"scheme": "prsindia"
},
{
"identifier": "Q16897877",
"scheme": "wikidata"
}
],
"image": "http://164.100.47.132/mpimage/photo/4655.jpg",
"images": [
{
"url": "http://164.100.47.132/mpimage/photo/4655.jpg"
}
],
"links": [
{
"note": "Wikipedia (en)",
"url": "https://en.wikipedia.org/wiki/Hariom_Singh_Rathore"
}
],
"name": "Yavatmal-Washim",
"other_names": [
{
"lang": "en",
"name": "Yavatmal-Washim Lok Sabha constituency",
"note": "multilingual"
},
{
"lang": "hi",
"name": "यवतमाल-वाशिम लोक सभा निरà¥à¤µà¤¾à¤šà¤¨ कà¥à¤·à¥‡à¤¤à¥à¤°",
<?xml version="1.0" encoding="UTF-8"?> {
"posts": [
],
"persons": [
{
"birth_date": "1957-08-09",
"contact_details": [
{
"type": "email",
"value": "hariomsingh.rathore#sansad.nic.in"
}
],
"email": "hariomsingh.rathore#sansad.nic.in",
"gender": "male",
"id": "0094ff14-ff6c-440a-96fc-f0bd68068569",
"identifiers": [
{
"identifier": "4655",
"scheme": "everypolitician_legacy"
},
{
"identifier": "hariomsinghrathore",
"scheme": "prsindia"
},
{
"identifier": "Q16897877",
"scheme": "wikidata"
}
],
"image": "http://164.100.47.132/mpimage/photo/4655.jpg",
"images": [
{
"url": "http://164.100.47.132/mpimage/photo/4655.jpg"
}
],
"links": [
{
"note": "Wikipedia (en)",
"url": "https://en.wikipedia.org/wiki/Hariom_Singh_Rathore"
}
],
"name": "Rathore, Shri Hariom Singh",
"other_names": [
{
"lang": "en",
"name": "Hariom Singh Rathore",
"note": "multilingual"
},
{
"lang": "gu",
"name": "હરિઓમ સિંહ રાઠૌડ઼",
"note": "multilingual"
}
]
},
{
"birth_date": "1975-09-10",
"contact_details": [
{
"type": "email",
"value": "ravneetbittu#gmail.com"
}
],
"email": "ravneetbittu#gmail.com",
"family_name": "Singh",
"gender": "male",
"id": "01727319-7f2b-465b-825c-1d7a94a54f70",
"identifiers": [
{
"identifier": "4429",
"scheme": "everypolitician_legacy"
},
{
"identifier": "ravneetsingh",
"scheme": "prsindia"
},
],
"image": "http://164.100.47.132/mpimage/photo/4429.jpg",
"images": [
{
"url": "http://164.100.47.132/mpimage/photo/4429.jpg"
}
],
"links": [
{
"note": "Wikipedia (en)",
"url": "https://en.wikipedia.org/wiki/Ravneet_Singh_Bittu"
},
{
"note": "Wikipedia (pa)",
"url": "https://pa.wikipedia.org/wiki/ਰਵਨੀਤ_ਸਿੰਘ"
}
],
"name": "Singh, Shri Ravneet",
"other_names": [
"note": "multilingual"
},
{
"lang": "mr",
"name": "यवतमाळ-वाशिम (लोकसभा मतदारसंघ)",
"note": "multilingual"
},
{
"lang": "ta",
{
"lang": "en",
"name": "Ravneet Singh",
"note": "multilingual"
},
{
"lang": "te",
"name": "రవనీతౠసింగౠబిటà±à°Ÿà±‚",
"note": "multilingual"
}
]
},
{
"birth_date": "1958-09-07",
"contact_details": [
{
"type": "email",
"value": "bhairon.prasad#sansad.nic.in"
}
],
"email": "bhairon.prasad#sansad.nic.in",
"family_name": "Mishra",
"gender": "male",
"id": "02670d6a-6b60-4e7b-b0cd-b4fc7d6c3bca",
"identifiers": [
{
"identifier": "4626",
"scheme": "everypolitician_legacy"
},
{
"identifier": "Q16902096",
"scheme": "wikidata"
}
],
"image": "http://164.100.47.132/mpimage/photo/4626.jpg",
"images": [
{
"url": "http://164.100.47.132/mpimage/photo/4626.jpg"
}
],
"links": [
{
"note": "Wikipedia (en)",
"url": "https://en.wikipedia.org/wiki/Bhairon_Prasad_Mishra"
},
{
"note": "Wikipedia (hi)",
"url": "https://hi.wikipedia.org/wiki/भैरों_पà¥à¤°à¤¸à¤¾à¤¦_मिशà¥à¤°"
}
],
"name": "யவதà¯à®®à®¾à®³à¯-வாசிம௠மகà¯à®•à®³à®µà¯ˆà®¤à¯ தொகà¯à®¤à®¿",
"note": "multilingual"
}
],
"type": "constituency"
},
{
"id": "zahirabad",
"identifiers": [
{
"identifier": "Q8064692",
"scheme": "wikidata"
}
],
"name": "Zahirabad",
"other_names": [
{
"lang": "en",
"name": "Zahirabad Lok Sabha constituency",
"note": "multilingual"
},
{
"lang": "hi",
"name": "ज़हीराबाद लोक सभा निरà¥à¤µà¤¾à¤šà¤¨ कà¥à¤·à¥‡à¤¤à¥à¤° समà¥à¤ªà¤¾à¤¦à¤¨",
"note": "multilingual"
},
{
"lang": "mr",
"name": "à¤à¤¹à¥€à¤°à¤¾à¤¬à¤¾à¤¦ (लोकसभा मतदारसंघ)",
"note": "multilingual"
},
{
"lang": "ta",
"name": "ஜஹீராபாதà¯",
"note": "multilingual"
},
{
"lang": "te",
"name": "జహీరాబాదౠలోకసభ నియోజకవరà±à°—à°‚",
"note": "multilingual"
}
],
"type": "constituency"
}
]
}
Can any one please help me in doing that. Thanks.
Your json is not an array of roots. The json is a single object with multiple nested objects inside a single root.
{
"posts": [
],
"persons": [
{ "birth_date":
}
Since it's not returning an array of objects, you need to deserialize to a singular root.
var root = JsonConvert.DeserializeObject<Root>(jsonString);

Problem with parsing JavaScript objects to C#

I'm parsing js file containing object values to C# objects. For now - I've converted JS code to JSON and then tried to convert to C# object.
I'm having problem with coming up to idea of how to generate objects in C#. I've tried doing multiple various tries, mostly with Dictionaries (Dictionary<string,[object]). I Googled, visited SO in multiple questions, no success for now - all my ideas resulted in null object.
Important note - I can't change the source of JS, can change anything after that.
Latest objects idea:
public class SingleFarm
{
public List<string> Modules { get; set; }
public List<string> Servers { get; set; }
}
public class SingleEnv
{
public Dictionary<string, SingleFarm> Farms { get; set; }
}
public class FarmsModel
{
public Dictionary<string, SingleEnv> FarmsModel { get; set; }
}
Parsing code:
var farmsText = File.ReadAllText(filePath);
//using Jurassic
var engine = new ScriptEngine();
var result = engine.Evaluate(farmsText);
var json = JSONObject.Stringify(engine, result);
var parsed = JsonConvert.DeserializeObject<FarmsModel>(json);
JS file source:
var environments = {};
environments['ENV1'] = {
"WWW": {
"Modules": [
"module21"
],
"Servers": [
"a-1"
]
}
};
environments['ENV2'] = {
"FARM1": {
"Modules": [
"module41"
],
"Servers": [
"s1",
"s2"
]
},
"FARM2": {
"Modules": [
"module11"
],
"Servers": [
""
]
},
"FARM3": {
"Modules": [
"module1"
],
"Servers": [
""
]
}
};
environments['ENV3'] = {
"FARM1": {
"Modules": [
"module10"
],
"Servers": [
"server1"
]
},
"FARM2": {
"Modules": [
"module22"
],
"Servers": [
""
]
},
"FARM3": {
"Modules": [
"module33"
],
"Servers": [
"server3"
]
}
};
JSON looks as follows:
{
"ENV1": {
"WWW": {
"Modules": [
"module21"
],
"Servers": [
"a-1"
]
}
},
"ENV2": {
"FARM1": {
"Modules": [
"module41"
],
"Servers": [
"s1",
"s2"
]
},
"FARM2": {
"Modules": [
"module11"
],
"Servers": [
""
]
},
"FARM3": {
"Modules": [
"module1"
],
"Servers": [
""
]
}
},
"ENV3": {
"FARM1": {
"Modules": [
"module10"
],
"Servers": [
"server1"
]
},
"FARM2": {
"Modules": [
"module22"
],
"Servers": [
""
]
},
"FARM3": {
"Modules": [
"module33"
],
"Servers": [
"server3"
]
}
}
}
Do you have any ideas?
You shouldn't be trying to serialize dictionaries to objects since it will try to map the property names.
If you use
var parsed = JsonConvert.DeserializeObject<Dictionary<string, Dictionary<string, SingleFarm>>>(json);
It should work. Fiddle

Merge Multiple JSON and Multiple level of Group By in C#

I'm trying to merge multiple JSON files which has the same type of data inside them and fetch the merged data from those JSONs. For example below are two JSON files.
JSON 1
[
{
"Name": "Sample1",
"Data": [
{
"Name": "Sample1 Sub1",
"Data": [
{
"Name": "XXX",
"ID": ["278924"]
}
]
}
]
},
{
"Name": "Sample2",
"Data": [
{
"Name": "Sample2 Sub1",
"Data": [
{
"Name": "XXX",
"ID": ["278378"]
},
{
"Name": "YYY",
"ID": ["278289"]
}
]
}
]
}
]
JSON 2
[
{
"Name": "Sample1",
"Data": [
{
"Name": "Sample1 Sub1",
"Data": [
{
"Name": "XXX",
"ID": ["357896"]
}
]
}
]
},
{
"Name": "Sample2",
"Data": [
{
"Name": "Sample2 Sub1",
"Data": [
{
"Name": "XXX",
"ID": ["356842"]
},
{
"Name": "YYY",
"ID": ["357123"]
}
]
}
]
}
]
I'm expecting the output to be in the below format.
[
{
"Name": "Sample1",
"Data": [
{
"Name": "Sample1 Sub1",
"Data": [
{
"Name": "XXX",
"ID": ["278924, 357896"]
}
]
}
]
},
{
"Name": "Sample2",
"Data": [
{
"Name": "Sample2 Sub1",
"Data": [
{
"Name": "XXX",
"ID": ["278378,356842"]
},
{
"Name": "YYY",
"ID": ["278289,357123"]
}
]
}
]
}
]
I'm not sure where to start this. I tried groupby for multiple level but couldn't able to fetch the result in expected format.
Any help would be appreciated.

Deserializing Import Io JSON with multiple objects

Morning all,
Im using Newtonsoft.JSON to deserialize a JSON response from Import Io. I have successfully used http://json2csharp.com/ to build a data model successfully in one instance to gain access to the data, however in that instance only one JSON object was returned. I am now working on a new piece of JSON that is being returned with multiple objects and i'm hitting issues with the following error message:
Newtonsoft.Json.JsonSerializationException: Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'System.Collections.Generic.List`1[OSP.Shop_DTO+RootObject]' 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 'url', line 1, position 7."
My JSON looks like this:
{
"url": "http://www.myurl.com/-shop-item.html",
"result": {
"extractorData": {
"url": "http://www.myurl.com/-shop-item.html",
"resourceId": "1db49f66afc2f234cb5ab470f0c39e0c",
"data": [
{
"group": [
{
"BN_shop_1KG": [
{
"text": "$36.00"
}
],
"BN_shop_2KG": [
{
"text": "$69"
}
],
"BN_shop_3KG": [
{
"text": "$97"
}
],
"BN_VEGE_5KG": [
{
"text": "3 KG = $97"
}
],
"BN_shop_4KG": [
{
"text": "$124"
}
],
"BN_shop_5KG": [
{
"text": "$149"
}
]
}
]
}
]
},
"pageData": {
"resourceId": "1db49f66afc2f234cb5ab470f0c39e0c",
"statusCode": 200,
"timestamp": 1476432605275
},
"timestamp": 1476432606594,
"sequenceNumber": 0
}
}{
"url": "http://www.myurl.com/-shop-concentrate.html",
"result": {
"extractorData": {
"url": "http://www.myurl.com/-shop-concentrate.html",
"resourceId": "dd4837cc7d0085eae005243c7bd8ca8a",
"data": [
{
"group": [
{
"BN_shop_1KG": [
{
"text": "$27.00"
}
],
"BN_shop_2KG": [
{
"text": "$49"
}
],
"BN_shop_3KG": [
{
"text": "$69"
}
],
"BN_VEGE_5KG": [
{
"text": "3 KG = $69"
}
],
"BN_shop_4KG": [
{
"text": "$84"
}
],
"BN_shop_5KG": [
{
"text": "$99"
}
]
}
]
}
]
},
"pageData": {
"resourceId": "dd4837cc7d0085eae005243c7bd8ca8a",
"statusCode": 200,
"timestamp": 1476432604237
},
"timestamp": 1476432605124,
"sequenceNumber": 1
}
}{
"url": "http://www.myurl.comshop-matrix.html",
"result": {
"extractorData": {
"url": "http://www.myurl.comshop-matrix.html",
"resourceId": "deee30ffa0098e017a06c0e0c805e133",
"data": [
{
"group": [
{
"BN_shop_1KG": [
{
"text": "$29.00"
}
],
"BN_shop_2KG": [
{
"text": "$56"
}
],
"BN_shop_3KG": [
{
"text": "$79"
}
],
"BN_VEGE_5KG": [
{
"text": "3 KG = $79"
}
],
"BN_shop_4KG": [
{
"text": "$99"
}
],
"BN_shop_5KG": [
{
"text": "$119"
}
]
}
]
}
]
},
"pageData": {
"resourceId": "deee30ffa0098e017a06c0e0c805e133",
"statusCode": 200,
"timestamp": 1476432602408
},
"timestamp": 1476432603204,
"sequenceNumber": 2
}
}{
"url": "http://www.myurl.comsoy-shop-item.html",
"result": {
"extractorData": {
"url": "http://www.myurl.comsoy-shop-item.html",
"resourceId": "5593aad40f95ba868626e47a1b550813",
"data": [
{
"group": [
{
"BN_shop_1KG": [
{
"text": "$25.00"
}
],
"BN_shop_2KG": [
{
"text": "$45"
}
],
"BN_shop_3KG": [
{
"text": "$89"
}
],
"BN_VEGE_5KG": [
{
"text": "5 KG = $89"
}
],
"BN_shop_4KG": [
{
"text": "$175"
}
],
"BN_shop_5KG": [
{
"text": "$339"
}
]
}
]
}
]
},
"pageData": {
"resourceId": "5593aad40f95ba868626e47a1b550813",
"statusCode": 200,
"timestamp": 1476432602479
},
"timestamp": 1476432603847,
"sequenceNumber": 3
}
}{
"url": "http://www.myurl.compea-shop.html",
"result": {
"extractorData": {
"url": "http://www.myurl.compea-shop.html",
"resourceId": "f91e05d0265ab5a5f7f948c57a05bced",
"data": [
{
"group": [
{
"BN_shop_1KG": [
{
"text": "$25.00"
}
],
"BN_shop_2KG": [
{
"text": "$45"
}
],
"BN_shop_3KG": [
{
"text": "$89"
}
],
"BN_VEGE_5KG": [
{
"text": "5 KG = $89"
}
],
"BN_shop_4KG": [
{
"text": "$169"
}
],
"BN_shop_5KG": [
{
"text": "$319"
}
]
}
]
}
]
},
"pageData": {
"resourceId": "f91e05d0265ab5a5f7f948c57a05bced",
"statusCode": 200,
"timestamp": 1476432605227
},
"timestamp": 1476432606451,
"sequenceNumber": 4
}
}
Model looks like this:
public class BNShop1KG
{
public string text { get; set; }
}
public class BNShop2KG
{
public string text { get; set; }
}
public class BNShop3KG
{
public string text { get; set; }
}
public class BNVEGE5KG
{
public string text { get; set; }
}
public class BNShop4KG
{
public string text { get; set; }
}
public class BNShop5KG
{
public string text { get; set; }
}
public class Group
{
public List<BNShop1KG> BN_shop_1KG { get; set; }
public List<BNShop2KG> BN_shop_2KG { get; set; }
public List<BNShop3KG> BN_shop_3KG { get; set; }
public List<BNVEGE5KG> BN_VEGE_5KG { get; set; }
public List<BNShop4KG> BN_shop_4KG { get; set; }
public List<BNShop5KG> BN_shop_5KG { get; set; }
}
public class Datum
{
public List<Group> group { get; set; }
}
public class ExtractorData
{
public string url { get; set; }
public string resourceId { get; set; }
public List<Datum> data { get; set; }
}
public class PageData
{
public string resourceId { get; set; }
public int statusCode { get; set; }
public long timestamp { get; set; }
}
public class Result
{
public ExtractorData extractorData { get; set; }
public PageData pageData { get; set; }
public long timestamp { get; set; }
public int sequenceNumber { get; set; }
}
public class RootObject
{
public string url { get; set; }
public Result result { get; set; }
}
In my code i'm trying to get access to the 'text' value in 'BN_shop_1KG' from the first JSON object. This item:
"BN_shop_1KG": [
{
"text": "$36.00"
}
I've tried the following in my code, but am receiving the error outlined above. From what I understand reading some previous threads about this is that I will need to use a deserialize into List to handle JSON received in this manner:
List<Shop_DTO.RootObject> obj = JsonConvert.DeserializeObject<List<Shop_DTO.RootObject>>(_rawHtmlResult);
var price = obj.First().result.extractorData.data[0].group[0].BN_shop_1kg[0].text;
Debug.WriteLine("Price for item {0}", price);
Any help would be hugely appreciated, i've been stuck on this for a couple of days :(
The Json file is invalid. Since it contains multiple Json object, it should be a Json array which needs to be wrapped by [], and another thing is between each Json object, you need ","
var jsonText = #"[{
""url"": ""http://www.myurl.com/-shop-item.html"",
""result"": {
""extractorData"": {
""url"": ""http://www.myurl.com/-shop-item.html"",
""resourceId"": ""1db49f66afc2f234cb5ab470f0c39e0c"",
""data"": [
{
""group"": [
{
""BN_shop_1KG"": [
{
""text"": ""$36.00""
}
],
""BN_shop_2KG"": [
{
""text"": ""$69""
}
],
""BN_shop_3KG"": [
{
""text"": ""$97""
}
],
""BN_VEGE_5KG"": [
{
""text"": ""3 KG = $97""
}
],
""BN_shop_4KG"": [
{
""text"": ""$124""
}
],
""BN_shop_5KG"": [
{
""text"": ""$149""
}
]
}
]
}
]
},
""pageData"": {
""resourceId"": ""1db49f66afc2f234cb5ab470f0c39e0c"",
""statusCode"": 200,
""timestamp"": 1476432605275
},
""timestamp"": 1476432606594,
""sequenceNumber"": 0
}
},{
""url"": ""http://www.myurl.com/-shop-concentrate.html"",
""result"": {
""extractorData"": {
""url"": ""http://www.myurl.com/-shop-concentrate.html"",
""resourceId"": ""dd4837cc7d0085eae005243c7bd8ca8a"",
""data"": [
{
""group"": [
{
""BN_shop_1KG"": [
{
""text"": ""$27.00""
}
],
""BN_shop_2KG"": [
{
""text"": ""$49""
}
],
""BN_shop_3KG"": [
{
""text"": ""$69""
}
],
""BN_VEGE_5KG"": [
{
""text"": ""3 KG = $69""
}
],
""BN_shop_4KG"": [
{
""text"": ""$84""
}
],
""BN_shop_5KG"": [
{
""text"": ""$99""
}
]
}
]
}
]
},
""pageData"": {
""resourceId"": ""dd4837cc7d0085eae005243c7bd8ca8a"",
""statusCode"": 200,
""timestamp"": 1476432604237
},
""timestamp"": 1476432605124,
""sequenceNumber"": 1
}
},{
""url"": ""http://www.myurl.comshop-matrix.html"",
""result"": {
""extractorData"": {
""url"": ""http://www.myurl.comshop-matrix.html"",
""resourceId"": ""deee30ffa0098e017a06c0e0c805e133"",
""data"": [
{
""group"": [
{
""BN_shop_1KG"": [
{
""text"": ""$29.00""
}
],
""BN_shop_2KG"": [
{
""text"": ""$56""
}
],
""BN_shop_3KG"": [
{
""text"": ""$79""
}
],
""BN_VEGE_5KG"": [
{
""text"": ""3 KG = $79""
}
],
""BN_shop_4KG"": [
{
""text"": ""$99""
}
],
""BN_shop_5KG"": [
{
""text"": ""$119""
}
]
}
]
}
]
},
""pageData"": {
""resourceId"": ""deee30ffa0098e017a06c0e0c805e133"",
""statusCode"": 200,
""timestamp"": 1476432602408
},
""timestamp"": 1476432603204,
""sequenceNumber"": 2
}
},{
""url"": ""http://www.myurl.comsoy-shop-item.html"",
""result"": {
""extractorData"": {
""url"": ""http://www.myurl.comsoy-shop-item.html"",
""resourceId"": ""5593aad40f95ba868626e47a1b550813"",
""data"": [
{
""group"": [
{
""BN_shop_1KG"": [
{
""text"": ""$25.00""
}
],
""BN_shop_2KG"": [
{
""text"": ""$45""
}
],
""BN_shop_3KG"": [
{
""text"": ""$89""
}
],
""BN_VEGE_5KG"": [
{
""text"": ""5 KG = $89""
}
],
""BN_shop_4KG"": [
{
""text"": ""$175""
}
],
""BN_shop_5KG"": [
{
""text"": ""$339""
}
]
}
]
}
]
},
""pageData"": {
""resourceId"": ""5593aad40f95ba868626e47a1b550813"",
""statusCode"": 200,
""timestamp"": 1476432602479
},
""timestamp"": 1476432603847,
""sequenceNumber"": 3
}
},{
""url"": ""http://www.myurl.compea-shop.html"",
""result"": {
""extractorData"": {
""url"": ""http://www.myurl.compea-shop.html"",
""resourceId"": ""f91e05d0265ab5a5f7f948c57a05bced"",
""data"": [
{
""group"": [
{
""BN_shop_1KG"": [
{
""text"": ""$25.00""
}
],
""BN_shop_2KG"": [
{
""text"": ""$45""
}
],
""BN_shop_3KG"": [
{
""text"": ""$89""
}
],
""BN_VEGE_5KG"": [
{
""text"": ""5 KG = $89""
}
],
""BN_shop_4KG"": [
{
""text"": ""$169""
}
],
""BN_shop_5KG"": [
{
""text"": ""$319""
}
]
}
]
}
]
},
""pageData"": {
""resourceId"": ""f91e05d0265ab5a5f7f948c57a05bced"",
""statusCode"": 200,
""timestamp"": 1476432605227
},
""timestamp"": 1476432606451,
""sequenceNumber"": 4
}
}]"
Then you can use your code to get the price. Btw, there is a typo in your code, BN_shop_1kg is supposed to be BN_shop_1KG

How can I debug the assignment of an object to a class?

I am trying to assign this object as follows:
RootObject aaa = new RootObject() {
"word": "example",
"results": [
{
"definition": "a representative form or pattern",
"partOfSpeech": "noun",
"synonyms": [
"model"
],
"typeOf": [
"representation",
"internal representation",
"mental representation"
],
"hasTypes": [
"prefiguration",
"archetype",
"epitome",
"guide",
"holotype",
"image",
"loadstar",
"lodestar",
"microcosm",
"original",
"paradigm",
"pilot",
"prototype",
"template",
"templet",
"type specimen"
],
"derivation": [
"exemplify"
],
"examples": [
"I profited from his example"
]
},
{
"definition": "something to be imitated",
"partOfSpeech": "noun",
"synonyms": [
"exemplar",
"good example",
"model"
],
"typeOf": [
"ideal"
],
"hasTypes": [
"pacemaker",
"pattern",
"beauty",
"prodigy",
"beaut",
"pacesetter"
],
"derivation": [
"exemplify",
"exemplary"
]
},
{
"definition": "an occurrence of something",
"partOfSpeech": "noun",
"synonyms": [
"case",
"instance"
],
"typeOf": [
"happening",
"natural event",
"occurrence",
"occurrent"
],
"hasTypes": [
"clip",
"mortification",
"piece",
"time",
"humiliation",
"bit"
],
"derivation": [
"exemplify"
],
"examples": [
"but there is always the famous example of the Smiths"
]
},
{
"definition": "an item of information that is typical of a class or group",
"partOfSpeech": "noun",
"synonyms": [
"illustration",
"instance",
"representative"
],
"typeOf": [
"information"
],
"hasTypes": [
"excuse",
"apology",
"specimen",
"case in point",
"sample",
"exception",
"quintessence",
"precedent"
],
"derivation": [
"exemplify",
"exemplary"
],
"examples": [
"this patient provides a typical example of the syndrome",
"there is an example on page 10"
]
},
{
"definition": "punishment intended as a warning to others",
"partOfSpeech": "noun",
"synonyms": [
"deterrent example",
"lesson",
"object lesson"
],
"typeOf": [
"monition",
"admonition",
"word of advice",
"warning"
],
"derivation": [
"exemplary"
],
"examples": [
"they decided to make an example of him"
]
},
{
"definition": "a task performed or problem solved in order to develop skill or understanding",
"partOfSpeech": "noun",
"synonyms": [
"exercise"
],
"typeOf": [
"lesson"
],
"examples": [
"you must work the examples at the end of each chapter in the textbook"
]
}
],
"syllables": {
"count": 3,
"list": [
"ex",
"am",
"ple"
]
},
"pronunciation": {
"all": "ɪɡ'zæmpəl"
}
}
Here are the classes I defined:
public class Result
{
public string definition { get; set; }
public string partOfSpeech { get; set; }
public List<string> synonyms { get; set; }
public List<string> typeOf { get; set; }
public List<string> hasTypes { get; set; }
public List<string> derivation { get; set; }
public List<string> examples { get; set; }
}
public class Syllables
{
public int count { get; set; }
public List<string> list { get; set; }
}
public class Pronunciation
{
public string all { get; set; }
}
public class RootObject
{
public string word { get; set; }
public List<Result> results { get; set; }
public Syllables syllables { get; set; }
public Pronunciation pronunciation { get; set; }
}
I'm getting syntax errors starting on the first line with "word".
Is there a way I can debug this or at least find out what is causing the problem?
Try
RootObject aaa = JsonConvert.DeserializeObject<RootObject>(
put_your_string_from_the_question);
Download Json.Net from nuget.org
You simply cannot assign JSON object to C# Object. You need to assign properties of your class and DeSerialize that to JSON format.
in your case you should do like this way:
var roorObj= new RootObject()
{
word ="your word",
//
//
}
Then to get the JOSN string, use this:
RootObject aaa = JsonConvert.DeserializeObject<RootObject>(roorObj);
Hope you will get some idea to play with C# object and JSON.

Categories