Can not deserialize json to Dictionary<string, List<Purchase>> - c#

I can not deserialize json to Dictionary<string, List<Purchas>> in C#.
Here is my .json:
{
"Ukraine": {
{
"Credits": 500,
"Name": "Clever goat",
"Price": {
"Amount": 100,
"Currency": "UAH"
}
},
{
"Credits": 1000,
"Name": "Smart hare",
"Price": {
"Amount": 190,
"Currency": "UAH"
}
}
},
"USA": {
{
"Credits": 500,
"Name": "Clever goat",
"Price": {
"Amount": 10,
"Currency": "USD"
}
},
{
"Credits": 1000,
"Name": "Smart hare",
"Price": {
"Amount": 19,
"Currency": "USD"
}
}
}
}
Here is my Purchase class:
public class Price
{
public int Amount { get; set; }
public string Currency { get; set; }
}
public class Purchase
{
public int Credits { get; set; }
public string Name { get; set; }
public int Price { get; set; }
}
Here is how I am trying to deserialize it:
var countryToPurchases = JsonConvert.DeserializeObject<Dictionary<string, List<Purchase>>>(dataJSON);
Here is the error I am getting:
JsonReaderException: Invalid property identifier character: {. Path 'Ukraine', line 3, position 4.
Newtonsoft.Json.JsonTextReader.ParseProperty()
What am I missing here?

You have two problems here:
The Json is invalid. There should be arrays there but there aren't.
A valid json would look like this:
[
{
"Ukraine": [
{
"Credits": 500,
"Name": "Clever goat",
"Price": {
"Amount": 100,
"Currency": "UAH"
}
},
{
"Credits": 1000,
"Name": "Smart hare",
"Price": {
"Amount": 190,
"Currency": "UAH"
}
}
]
},
{
"USA": [
{
"Credits": 500,
"Name": "Clever goat",
"Price": {
"Amount": 10,
"Currency": "USD"
}
},
{
"Credits": 1000,
"Name": "Smart hare",
"Price": {
"Amount": 19,
"Currency": "USD"
}
}
]
}
]
The Price property should be of type Price, not int.
public class Purchase
{
public int Credits { get; set; }
public string Name { get; set; }
public Price Price { get; set; }
}

please check this json and see if it's works :
{
"Ukraine": {
"Credits": 500,
"Name": "Clever goat",
"Price": {
"Amount": 100,
"Currency": "UAH"
}
},
"USA": {
"Credits": 500,
"Name": "Clever goat",
"Price": {
"Amount": 10,
"Currency": "USD"
}
}
}

Related

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

Deserialize json data c#

I want to deserialize JSON response received by Telegram Bot API by getUpdate() method.
JSON data:
{
"ok": true,
"result": [
{
"update_id": 920493886,
"message": {
"message_id": 123,
"from": {
"id": 219633031,
"first_name": "Shreeeeeee",
"username": "Winner7777"
},
"chat": {
"id": 219633031,
"first_name": "Shreeeeeee",
"username": "Winner7777",
"type": "private"
},
"date": 1472457375,
"text": "Aata aala"
}
},
{
"update_id": 920493887,
"message": {
"message_id": 124,
"from": {
"id": 219633031,
"first_name": "Shreeeeeee",
"username": "Winner7777"
},
"chat": {
"id": 219633031,
"first_name": "Shreeeeeee",
"username": "Winner7777",
"type": "private"
},
"date": 1472457387,
"text": "Jeva tuzyakadun reply aala tevha"
}
},
{
"update_id": 920493888,
"message": {
"message_id": 125,
"from": {
"id": 219633031,
"first_name": "Shreeeeeee",
"username": "Winner7777"
},
"chat": {
"id": 219633031,
"first_name": "Shreeeeeee",
"username": "Winner7777",
"type": "private"
},
"date": 1472457443,
"text": "Deposite"
}
},
{
"update_id": 920493889,
"message": {
"message_id": 127,
"from": {
"id": 201520743,
"first_name": "Chandrakant",
"last_name": "Kumathekar",
"username": "chandrakant_k"
},
"chat": {
"id": 201520743,
"first_name": "Chandrakant",
"last_name": "Kumathekar",
"username": "chandrakant_k",
"type": "private"
},
"date": 1472457645,
"text": "\/menu",
"entities": [
{
"type": "bot_command",
"offset": 0,
"length": 5
}
]
}
},
{
"update_id": 920493890,
"message": {
"message_id": 128,
"from": {
"id": 219633031,
"first_name": "Shreeeeeee",
"username": "Winner7777"
},
"chat": {
"id": 219633031,
"first_name": "Shreeeeeee",
"username": "Winner7777",
"type": "private"
},
"date": 1472457670,
"text": "\/menu",
"entities": [
{
"type": "bot_command",
"offset": 0,
"length": 5
}
]
}
},
{
"update_id": 920493891,
"message": {
"message_id": 130,
"from": {
"id": 201520743,
"first_name": "Chandrakant",
"last_name": "Kumathekar",
"username": "chandrakant_k"
},
"chat": {
"id": 201520743,
"first_name": "Chandrakant",
"last_name": "Kumathekar",
"username": "chandrakant_k",
"type": "private"
},
"date": 1472457848,
"text": "Deposite"
}
},
{
"update_id": 920493892,
"message": {
"message_id": 132,
"from": {
"id": 201520743,
"first_name": "Chandrakant",
"last_name": "Kumathekar",
"username": "chandrakant_k"
},
"chat": {
"id": 201520743,
"first_name": "Chandrakant",
"last_name": "Kumathekar",
"username": "chandrakant_k",
"type": "private"
},
"date": 1472457883,
"text": "Deposite"
}
},
{
"update_id": 920493893,
"message": {
"message_id": 133,
"from": {
"id": 219633031,
"first_name": "Shreeeeeee",
"username": "Winner7777"
},
"chat": {
"id": 219633031,
"first_name": "Shreeeeeee",
"username": "Winner7777",
"type": "private"
},
"date": 1472468407,
"text": "\/menu",
"entities": [
{
"type": "bot_command",
"offset": 0,
"length": 5
}
]
}
},
{
"update_id": 920493894,
"message": {
"message_id": 134,
"from": {
"id": 219633031,
"first_name": "Shreeeeeee",
"username": "Winner7777"
},
"chat": {
"id": 219633031,
"first_name": "Shreeeeeee",
"username": "Winner7777",
"type": "private"
},
"date": 1472473070,
"text": "\/menu",
"entities": [
{
"type": "bot_command",
"offset": 0,
"length": 5
}
]
}
}
]
}
I have generated classes from json2csharp
public class From
{
public int id { get; set; }
public string first_name { get; set; }
public string last_name { get; set; }
public string username { get; set; }
}
public class Chat
{
public int id { get; set; }
public string first_name { get; set; }
public string last_name { get; set; }
public string type { get; set; }
public string username { get; set; }
}
public class Entity
{
public string type { get; set; }
public int offset { get; set; }
public int length { get; set; }
}
public class Message
{
public int message_id { get; set; }
public From from { get; set; }
public Chat chat { get; set; }
public int date { get; set; }
public string text { get; set; }
public List<Entity> entities { get; set; }
}
public class Result
{
public int update_id { get; set; }
public Message message { get; set; }
}
public class RootObject
{
public bool ok { get; set; }
public List<Result> result { get; set; }
}
using newtonsoft to deserialization
var d = Newtonsoft.Json.JsonConvert.DeserializeObject(rcvd_data);
what to do next? how to put this all at work? I am confused please help.
To deserialization use this code:
RootObject ro = JsonConvert.DeserializeObject<RootObject>(rcvd_data);
And in ro you have all data.
EXAMPLE
bool ok = ro.ok;
foreach(Result r in ro.result)
{
int uId = r.update_id;
Message m = r.message;
int msgId = m.message_id;
}
string data = #"{""ok"":true,""result"":[{""update_id"":920493886,...";
RootObject ro = Newtonsoft.Json.JsonConvert.DeserializeObject<RootObject>(data);
foreach (Result result in ro.result)
{
//two example fields
Console.WriteLine("update_id= " + result.update_id);
Console.WriteLine("message text= "+result.message.text);
}
Based on: https://yts.am/api/v2/list_movies.json (Source: https://yts.am/api)
I used: http://json2csharp.com/ to create classes
Copy and paste the result of the get request of postman to json2csharp to automatically create the classes that you need
And once created, i used them on a Service like this:
private string URL = "https://yts.am/api/v2/list_movies.json";
public async Task<List<Movie>> GetMovies()
{
var httpClient = new HttpClient();
var json = await httpClient.GetStringAsync(URL);
RootObject lista = Newtonsoft.Json.JsonConvert.DeserializeObject<RootObject>(json);
List<Movie> todoes = lista.data.movies;
return todoes;
}
I'm using Newtonsoft.Json
Try this way
var d = Newtonsoft.Json.JsonConvert.DeserializeObject<RootObject>(rcvd_‌​data);
then d will contain list of RootObject. By iterating using foreach you can get all the properties of RootObject model

Sort the sum aggregate - Nest (ElasticSearch)

The requirement is to group by Employee Id and Name and then aggregate the sum(salary). I have managed to get it to work. However, the final aggregated result has to be sorted in descending order of the sum. I tried couple of ways but the ordering doesn't seem to work.
var resp =
elasticClient.Search<Employee>(
k => k.Size(0).Query(q => q.QueryString(s => s.Query(query)))
.Aggregations(a => a.Terms("EmpId", v => v.Field(f => f.EmpId)
.Aggregations(aa => aa.Terms("EmpName",vv => vv.Field(ff => ff.EmpName).OrderDescending("Salary")
.Aggregations(aaa => aaa.Sum("Salary", sa => sa.Field(fff => fff.Salary))))))));
Employee Class:
public sealed class Employee
{
public long Id { get; set; }
public int DateId { get; set; }
public int? DivisionNumber { get; set; }
public string Manager { get; set; }
public decimal Salary { get; set; }
public int UpdateId { get; set; }
public DateTime UpdateTimestamp { get; set; }
public int EmpId { get; set; }
public string EmpName { get; set; }
}
Mapping:
POST /sample_employee
{
"mappings": {
"post":{
"properties": {
"empId": {
"type" : "long"
},
"empName": {
"type": "string",
"index": "not_analyzed"
},
"salary": {
"type": "float"
},
"dateId": {
"type": "long"
},
"divisionNumber":{
"type": "long"
}
}
}
}
}
Query for aggregation:
GET /sample_employee/_search
{
"size": 0,
"query": {
"query_string": {
"query": "dateId:(1780) AND divisionNumber:(1)"
}
},
"aggs": {
"EmpId": {
"terms": {
"field": "empId"
},
"aggs": {
"EmpName": {
"terms": {
"field": "empName",
"size": 30,
"order":{
"Amount.value": "desc"}
},
"aggs": {
"Amount": {
"sum": {
"field": "salary"
}
}
}
}
}
}
}
}
Response:
"aggregations": {
"EmpId": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": 10,
"doc_count": 1,
"EmpName": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": "Jerry Mathews",
"doc_count": 1,
"Amount": {
"value": 10000
}
}
]
}
},
{
"key": 11,
"doc_count": 1,
"EmpName": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": "Tom Raju",
"doc_count": 1,
"Amount": {
"value": 15000
}
}
]
}
},
{
"key": 12,
"doc_count": 1,
"EmpName": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": "Nel Gad",
"doc_count": 1,
"Amount": {
"value": 20000
}
}
]
}
},
{
"key": 13,
"doc_count": 1,
"EmpName": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": "Jim Thomas",
"doc_count": 1,
"Amount": {
"value": 25000
}
}
]
}
},
{
"key": 14,
"doc_count": 1,
"EmpName": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": "Amat Ahu",
"doc_count": 1,
"Amount": {
"value": 30000
}
}
]
}
}
]
}
}
}

Difficulty deserialising nested JSON

I am calling a REST service to get back a JSON structure.
The error I get is
Test method BarPanda.Web.Services.Test.PosServiceTests.GetMenu threw exception:
System.MissingMethodException: No parameterless constructor defined for this object.
The original JSON is as follows (partial)
{
"count": 3,
"limit": 50,
"_links": {
"self": {
"etag": "b49a27c7e7c663af8d6a736e24fac7f5",
"href": "https://api.omnivore.io/0.1/locations/gcBdM7TL/menu/categories/" ,
"profile": "https://panel.omnivore.io/docs/api#category_list" 
}
},
"_embedded": {
"categories": [
{
"id": "AdiRjiAp",
"name": "Drinks",
"_links": {
"items": {
"etag": "05dad4d734401321a4854cf4f0369102",
"href": "https://api.omnivore.io/0.1/locations/gcBdM7TL/menu/categories/AdiRjiAp/items/" ,
"profile": "https://panel.omnivore.io/docs/api#menu-item_list" 
},
"self": {
"etag": "05dad4d734401321a4854cf4f0369102",
"href": "https://api.omnivore.io/0.1/locations/gcBdM7TL/menu/categories/AdiRjiAp/" ,
"profile": "https://panel.omnivore.io/docs/api#category_retrieve" 
}
},
"_embedded": {
"items": [
{
"id": "gki84ia9",
"in_stock": true,
"modifier_groups_count": 0,
"name": "Soda",
"open": false,
"pos_id": "gki84ia9",
"price": 150,
"price_levels": [
{
"id": "Byineidy",
"price": 150
},
{
"id": "g4T4dTBj",
"price": 200
},
{
"id": "K6czkc8b",
"price": 250
}
],
"_links": {
"modifier_groups": {
"href": "https://api.omnivore.io/0.1/locations/gcBdM7TL/menu/items/gki84ia9/modifier_groups/" ,
"profile": "https://panel.omnivore.io/docs/api#modifier-group_list" 
},
"self": {
"etag": "c59b380aed5c1f33915b028b739df955",
"href": "https://api.omnivore.io/0.1/locations/gcBdM7TL/menu/items/gki84ia9/" ,
"profile": "https://panel.omnivore.io/docs/api#menu-item_retrieve" 
}
}
},
{
"id": "doTaLTyg",
"in_stock": true,
"modifier_groups_count": 0,
"name": "Orange Juice",
"open": false,
"pos_id": "doTaLTyg",
"price": 175,
"price_levels": [
{
"id": "L4iqKid8",
"price": 175
},
{
"id": "K6T8MTzb",
"price": 300
}
],
"_links": {
"modifier_groups": {
"href": "https://api.omnivore.io/0.1/locations/gcBdM7TL/menu/items/doTaLTyg/modifier_groups/" ,
"profile": "https://panel.omnivore.io/docs/api#modifier-group_list" 
},
"self": {
"etag": "d3ae9754edb321f18e192ebea446baeb",
"href": "https://api.omnivore.io/0.1/locations/gcBdM7TL/menu/items/doTaLTyg/" ,
"profile": "https://panel.omnivore.io/docs/api#menu-item_retrieve" 
}
}
}
]
}
},
I am trying to deserialize it with the following code and object classes
var response = _client.Execute(request);
var converter = new JsonDeserializer();
var menu = converter.Deserialize<PosMenu>(response);
PosMenu
[DataContract]
public class PosMenu
{
[DataMember]
public int VenueId { get; set; }
[DataMember]
public int count { get; set; }
[DataMember]
public PosMenuEmbedded _embedded { get; set; }
}
PosMenuEmbedded
[DataContract]
public class PosMenuEmbedded
{
[DataMember]
public long UniqueId { get; set; }
[DataMember]
public PosMenuCategory[] categories { get; set; }
[DataMember]
public int PosMenuId { get; set; }
}
PosMenuCategory
[DataContract]
public class PosMenuCategory
{
}
Note: I have taken all properties out of this class for now just to see if I could get it working with a blank class, but alas not.
If I comment out the line in PosMenuEmbedded
public PosMenuCategory[] categories { get; set; }
It succeeds. If I put it back in, it fails, even with an empty class.
Can anyone suggest why this might be?
[DataMember]
public List<PosMenuCategory> categories { get; set; }

Not able to convert string to json

i am getting measurements from withings and want to show them in graphs but not able to convert it to json. i also try JsonConvert.SerializeObject(myString) using Newtonsoft.dlland simple
System.Web.Script.Serialization.JavaScriptSerializer sr = new System.Web.Script.Serialization.JavaScriptSerializer();
sr.Serialize(myString);
but it is not converting.
My withings measurement string is as follows.
{
"status": 0,
"body": {
"updatetime": 1392764547,
"measuregrps": [
{
"grpid": 17945868,
"attrib": 0,
"date": 139984270,
"category": 1,
"measures": [
{
"value": 72,
"type": 9,
"unit": 0
},
{
"value": 152,
"type": 10,
"unit": 7
},
{
"value": 87,
"type": 17,
"unit": 0
}
]
},
{
"grpid": 176587495,
"attrib": 0,
"date": 13915689,
"category": 1,
"measures": [
{
"value": 94,
"type": 9,
"unit": 0
},
{
"value": 145,
"type": 10,
"unit": 0
},
{
"value": 109,
"type": 11,
"unit": 0
}
]
},
{
"grpid": 179262494,
"attrib": 0,
"date": 1391369607,
"category": 1,
"measures": [
{
"value": 77,
"type": 9,
"unit": 0
},
{
"value": 121,
"type": 10,
"unit": 0
},
{
"value": 87,
"type": 11,
"unit": 0
}
]
},
{
"grpid": 179258492,
"attrib": 0,
"date": 1391171167,
"category": 1,
"measures": [
{
"value": 61,
"type": 9,
"unit": 0
},
{
"value": 107,
"type": 10,
"unit": 0
},
{
"value": 80,
"type": 11,
"unit": 0
}
]
},
{
"grpid": 179089150,
"attrib": 0,
"date": 1391167537,
"category": 1,
"measures": [
{
"value": 69,
"type": 9,
"unit": 0
},
{
"value": 112,
"type": 10,
"unit": 0
},
{
"value": 67,
"type": 11,
"unit": 0
}
]
},
{
"grpid": 179079661,
"attrib": 2,
"date": 1391164672,
"category": 1,
"measures": [
{
"value": 720,
"type": 1,
"unit": -1
}
]
},
{
"grpid": 17998560,
"attrib": 2,
"date": 146989672,
"category": 1,
"measures": [
{
"value": 284,
"type": 4,
"unit": -2
}
]
}
]
}
}
It seems, you want to deserialize your json string, not serialize:
var obj = JsonConvert.DeserializeObject<Withings.RootObject>(json);
public class Withings
{
public class Measure
{
public int value { get; set; }
public int type { get; set; }
public int unit { get; set; }
}
public class Measuregrp
{
public int grpid { get; set; }
public int attrib { get; set; }
public int date { get; set; }
public int category { get; set; }
public List<Measure> measures { get; set; }
}
public class Body
{
public int updatetime { get; set; }
public List<Measuregrp> measuregrps { get; set; }
}
public class RootObject
{
public int status { get; set; }
public Body body { get; set; }
}
}
JsonConvert.SerializeObject(myString) takes an object an returns a string. If you want to turn a string into an object you want to use Deserialize<T>(sting json). Given the arguments name in your sample is myString I would assume you're using the method wrong.
To deserialize you need an equivalent type like;
public class myObject
{
public int status { get; set; }
public Body body { get; set; }
}
public class Body
{
//other parameters ect
}
Your object model needs to exactly match the json in order by Deserialize<T> to behave correctly.

Categories