Reputation: 608
I have tried various examples i found on stackoverflow as well as official documentation from www.newtonsoft.com. I am struggling to deserialize/parse this
{
"@odata.context": "http://localhost/WebApi/getDataCompanyData",
"value": [
{
"ID": "001",
"Name": "Sample Company"
}
]
}
and this with an array
{
"@odata.context": "http://localhost/WebApi/getDataCustomerData",
"value": [
{
"CustomerNumber": "A001",
"ShortName": "Customer A"
},
{
"CustomerNumber": "B001",
"ShortName": "Customer B"
}
]
}
I tried classes generated using http://json2csharp.com/ and i even tried something as simple as this
dynamic jsonstring = IO.File.ReadAllText("DATA\\cprofile.json");
dynamic companydata = Json.Linq.JObject.Parse(jsonstring);
dynamic CustomerNumber = companydata("CustomerNumber");
Interaction.MsgBox(CustomerNumber);
I have not worked with this JSON structure before. Anyone have advice in dealing with this kind of JSON structure?
Note: this is in winforms
Upvotes: 0
Views: 65
Reputation: 247423
You can consider using JsonProperty
with Json.Net
For example the second snippet with the array
public class Value {
public string CustomerNumber { get; set; }
public string ShortName { get; set; }
}
public class RootObject {
[JsonProperty("@odata.context")]
public string OdataContext { get; set; }
public IList<Value> value { get; set; }
}
From there it is just to deserialize the JSON as expected.
var model = JsonConvert.DeserializeObject<RootObject>(json);
var context = model.OdataContext;
http://jsonutils.com/ has the ability to put the attributes on the class for you. The only catch was that you would need to manually rename any properties that do not conform to proper c# syntax.
Upvotes: 1