I have the following section of JSON coming from Shopify:
{
"orders": [
{
"total_line_items_price_set": {
"shop_money": {
"amount": "281.96",
"currency_code": "USD"
},
"presentment_money": {
"amount": "281.96",
"currency_code": "USD"
}
},
"total_discounts_set": {
"shop_money": {
"amount": "0.00",
"currency_code": "USD"
},
"presentment_money": {
"amount": "0.00",
"currency_code": "USD"
}
},
},
When I utilize the Paste JSON as Classes feature I am getting elements like "shop_money" and "presentment_money" repeated and numbered as individual classes.
public class Total_Line_Items_Price_Set
{
public Shop_Money shop_money { get; set; }
public Presentment_Money presentment_money { get; set; }
}
public class Shop_Money
{
public string amount { get; set; }
public string currency_code { get; set; }
}
public class Presentment_Money
{
public string amount { get; set; }
public string currency_code { get; set; }
}
public class Total_Discounts_Set
{
public Shop_Money1 shop_money { get; set; }
public Presentment_Money1 presentment_money { get; set; }
}
public class Shop_Money1
{
public string amount { get; set; }
public string currency_code { get; set; }
}
public class Presentment_Money1
{
public string amount { get; set; }
public string currency_code { get; set; }
}
I'm new to JSON and how it should be represented as classes in C# - this seems wrong. Is it possible to get a better result?
SOLUTION:
Based on the answer by Zakk Diaz I removed each class that was duplicated and numbered. Having done so I am successfully deserializing and making use of an array of order objects.
public class Orders
{
public Order[] orders { get; set; }
}
public class Order
{
// [...]
}
class Program
{
static void Main(string[] args)
{
// [...]
Orders testOrders = JsonConvert.DeserializeObject<Orders>(responseFromServer);
Console.WriteLine(testOrders.orders[0].id);
Console.WriteLine(testOrders.orders[0].total_line_items_price_set.shop_money.amount);
Console.WriteLine(testOrders.orders[0].line_items[1].price_set.shop_money.amount);
}
}
It looks fine for the most part, you can delete these classes and rename them in "Total_Discounts_Set"
public class Shop_Money1
{
public string amount { get; set; }
public string currency_code { get; set; }
}
public class Presentment_Money1
{
public string amount { get; set; }
public string currency_code { get; set; }
}
Related
I am having a very difficult time reaching some deeply nested objects in my JSON
I have a directory with roughly 500 JSON files that I need to read through, and output certain data from so I load the files like this:
public static void getJsonFiles()
{
int i = 0;
string directory = #"Z:\My_JSON_FILES\DataFilesForAnalysis\DataFilesAsJSON";
string[] jsonPath = Directory.GetFiles(directory, "*.json");
foreach(string item in jsonPath)
{
jsonReader(item, i);
i++;
}
}
Once I have the file loaded, I am reading it through File.ReadAllText so I am doing this:
public static void jsonReader(string item, int i)
{
string readJson = File.ReadAllText(item);
RootObject rootObj = JsonConvert.DeserializeObject<RootObject>(readJson);
var resReport = rootObj.ResultsReport;
...
I have created objects of all of the JSON using json2csharp, but when I try to access the deeply nested objects using dot notation (rootObj.ResultsReport.FinalReport.VariantProperties.VariantProperty.VariantName), I get an error 'Object does not contain a definition for FinalReport and no extension method FinalReport...'
My object definition looks like this:
public class VariantProperty
{
public string geneName { get; set; }
public string isVUS { get; set; }
public string variantName { get; set; }
}
public class VariantProperties
{
public string[] VariantProperty { get; set; }
}
public class FinalReport
{
public Object Application { get; set; }
public string ReportId { get; set; }
public string SampleName { get; set; }
public string Version { get; set; }
public Object Sample { get; set; }
public string PertinentNegatives { get; set; }
public Object Summaries { get; set; }
public Object VariantProperties { get; set; }
public Object Genes { get; set; }
public Object Trials { get; set; }
public Object References { get; set; }
public Object Signatures { get; set; }
public Object AAC { get; set; }
}
public class ResultsReport
{
public Object FinalReport { get; set; }
public Object VariantReport { get; set; }
}
public class RootObject
{
public Object ResultsReport { get; set; }
}
The JSON looks like this:
"ResultsReport": {
"CustomerInformation": null,
"FinalReport": {
"#xmlns:xsd": "http://www.w3.org/2001/XMLSchema",
"#StagingId": "XXXXXXXX",
"#clinicalId": "XXXXXXXX",
"Application": {
"ApplicationSettings": {
"ApplicationSetting": {
"Name": "Statement",
"Value": "XXXXXXXX"
}
}
},
"ReportId": "XXXXXXXX",
"SampleName": "XXXXXXXX",
"Version": "1",
"Sample": {
"FM_Id": "XXXXXXXX",
"SampleId": "XXXXXXXX",
"BlockId": "XXXXXXXX",
"TRFNumber": "XXXXXXXX",
"TestType": "XXXXXXXX",
"SpecFormat": "XXXXXXXX",
"ReceivedDate": "XXXXXXXX"
},
"PertinentNegatives": null,
"Summaries": {
"#alterationCount": "XXXXXXXX",
"#clinicalTrialCount": "XXXXXXXX",
"#resistiveCount": "XXXXXXXX",
"#sensitizingCount": "XXXXXXXX"
},
"VariantProperties": {
"VariantProperty": [
{
"#geneName": "BARD1",
"#isVUS": "true",
"#variantName": "P358_S364del"
},
{
"#geneName": "GATA2",
"#isVUS": "true",
"#variantName": "P161A"
},
{
"#geneName": "LRP1B",
"#isVUS": "true",
"#variantName": "V4109I"
},
{
"#geneName": "MLL2",
"#isVUS": "true",
"#variantName": "P1191L"
},
{
"#geneName": "NTRK1",
"#isVUS": "true",
"#variantName": "G18E"
},
{
"#geneName": "NUP98",
"#isVUS": "true",
"#variantName": "A447T"
},
{
"#geneName": "TET2",
"#isVUS": "true",
"#variantName": "D1121Y"
},
{
"#geneName": "WT1",
"#isVUS": "true",
"#variantName": "T377_G397>S"
}
]
}
What am I doing wrong? I've followed so many different examples but it just wont seem to work
Write properties like
public ResultsReport ResultsReport { get; set; }
public FinalReport FinalReport { get; set; }
You are using object as property type, thats wrong, it is not about JSON deserialization.
As Volkan said the issue isn't the JSON deserialization. I got your json working by structuring my classes like so:
public class VariantProperty
{
public string geneName { get; set; }
public string isVUS { get; set; }
public string variantName { get; set; }
}
public class VariantProperties
{
public List<VariantProperty> VariantProperty { get; set; }
}
public class FinalReport
{
public Object Application { get; set; }
public string ReportId { get; set; }
public string SampleName { get; set; }
public string Version { get; set; }
public Object Sample { get; set; }
public string PertinentNegatives { get; set; }
public Object Summaries { get; set; }
public VariantProperties VariantProperties { get; set; }
public Object Genes { get; set; }
public Object Trials { get; set; }
public Object References { get; set; }
public Object Signatures { get; set; }
public Object AAC { get; set; }
}
public class ResultsReport
{
public FinalReport FinalReport { get; set; }
}
public class RootObject
{
public ResultsReport ResultsReport { get; set; }
}
I have a JSON returning from web like this
{
"data": {
"normal_customer": {
"0": {
"id": 1,
"name": "ALPHY"
}
},
"1": {
"id": 2,
"name": "STEVEN"
}
},
"luxury_customer": {
"3": {
"id": 8,
"name": "DEV"
}
}
}
}
I have created c# classes
public class StandardCustomers
{
public List<CustomersDetails> Customers_Details { get; set; }
}
public class CustomersDetails
{
[JsonProperty("id")]
public int id { get; set; }
[JsonProperty("name")]
public string name { get; set; }
public class LuxuryCustomers
{
public List<CustomersDetails> Customers_Details { get; set; }
}
public class Data
{
public StandardCustomers standard_Customers { get; set; }
public LuxuryCustomers luxury_Customers { get; set; }
}
public class RootObject
{
public Data data { get; set; }
}
}
When I use deserialize the response from the website using below c# code
var result1 = JsonConvert.DeserializeObject<Data>(response);
but result1.luxury_customers contains customerdetails which is null.
As suggested by #hellostone, I have modified to rootdata, then also
result1.luxury_customers contains customerdetails is null.
Any idea how to deserialize to c# class
When we pasted Json to visual studio, it generated classes as below
public class Rootobject
{
public Data data { get; set; }
}
public class Data
{
public Standard_Customers standard_Customers { get; set; }
public Luxury_Customers luxury_Customers { get; set; }
}
public class Standard_Customers
{
public _0 _0 { get; set; }
public _1 _1 { get; set; }
public _2 _2 { get; set; }
public _4 _4 { get; set; }
public _5 _5 { get; set; }
}
public class _0
{
public int id { get; set; }
public string name { get; set; }
}
individual classes are generated in standard customers , can we use list for this
I guess the problem is that index in luxury_customers starting not from zero. Try to use Dictionary<string,CustomersDetails> in LuxuryCustomers instead List<CustomersDetails>.
I've managed to deserialize Json with this classes:
public class CustomersDetails
{
[JsonProperty("id")]
public int id { get; set; }
[JsonProperty("name")]
public string name { get; set; }
}
public class Data
{
public Dictionary<string, CustomersDetails> normal_customer { get; set; }
public Dictionary<string,CustomersDetails> luxury_customer { get; set; }
}
public class RootObject
{
public Data data { get; set; }
}
Deserialization code:
var result = JsonConvert.DeserializeObject<RootObject>(text);
P.S. I've remove one closing bracket after "ALPHY" element, to make Json valid, I hope it was typo and you're getting valid Json.
Your json string doesn't match with your defined classes. Your Json string should look like this if you want to preserve your class structure:
{
"data":{
"standard_Customers":{
"Customers_Details":[
{
"id":1,
"name":"ALPHY"
},
{
"id":2,
"name":"STEVEN"
}
]
},
"luxury_Customers":{
"Customers_Details":[
{
"id":8,
"name":"DEV"
}
]
}
}
}
Pay attention to the square brackets at the "Customers_Details" attribute.
Then the:
var result1 = JsonConvert.DeserializeObject<RootObject>(response);
call, will give you the right object back and it shouldn't be null, when using your class structure:
public class StandardCustomers
{
public List<CustomersDetails> Customers_Details { get; set; }
}
public class CustomersDetails
{
[JsonProperty("id")]
public int id { get; set; }
[JsonProperty("name")]
public string name { get; set; }
}
public class LuxuryCustomers
{
public List<CustomersDetails> Customers_Details { get; set; }
}
public class Data
{
public StandardCustomers standard_Customers { get; set; }
public LuxuryCustomers luxury_Customers { get; set; }
}
public class RootObject
{
public Data data { get; set; }
}
I'm using JSON.Net to try and deserialize some responses from facebook. Here's a snapshot of the data I'm reading in:
{
"data": [
{
"id": "123"
},
{
"id": "234"
}
],
"paging": {
"cursors": {
"before": "xxx",
"after": "xxx"
},
"next": "xxx"
}
}
classes:
class jsonDeserialize
{
public List<ListDetail> ListDetail { get; set; }
}
public class DataList
{
public string id { get; set; }
}
public class Paging
{
public List<string> cursors { get; set; }
public string next { get; set; }
}
public class Cursors
{
public string before { get; set; }
public string after { get; set; }
}
public class ListDetail
{
public List<Cursors> Cursors { get; set; }
public List<Paging> Articles { get; set; }
public List<DataList> DataList { get; set; }
}
I'm using this code to use the JSON.NET Deserialize function:
for some reason, result return null, please help me :(
var result = JsonConvert.DeserializeObject<jsonDeserialize>(jsonString);
Fix your classes defenition:
class jsonDeserialize
{
public List<DataList> data { get; set; }
public Paging paging { get; set; }
}
public class DataList
{
public string id { get; set; }
}
public class Paging
{
public Cursors cursors { get; set; }
public string next { get; set; }
}
public class Cursors
{
public string before { get; set; }
public string after { get; set; }
}
jsonDeserialize contains list of DataList, ListDetail can me removed, cursors is not an array, it's an object.
Omg, I've missed it is JSON from facebook and "solved" reversed problem. I'll keep it here, though, just in case
First, as quick remark, your deserialization code here must look smth like var result = JsonConvert.DeserializeObject<jsonDeserialize>(jsonString);
Your JSON does not correspond to code.
If you want deserialize to jsonDeserialize class your json must look like that
(pay close attention to [] and {})
{
"ListDetail" :
[
{
"Cursors":
[
{
"before" : "value",
"after" : "value"
}
],
"Articles":
[
{
"Cursors":
[
"cursor1",
"cursor2"
],
"next" : "somenext"
}
],
"DataList":
[
{
"id" : "someid"
},
{
"id" : "someid2"
}
]
}
]
}
class jsonDeserialize
{
public List<DataList> data { get; set; }
public Paging paging { get; set; }
}
public class DataList
{
public string id { get; set; }
}
public class Paging
{
public Cursors cursors { get; set; }
public string next { get; set; }
}
public class Cursors
{
public string before { get; set; }
public string after { get; set; }
}
var result=JsonConvert.DeserializeObject<jsonDeserialize>(jsonformat.Substring(0,jsonformat.Length));
Use it it will give you write result. beacuse data and paging treated as properties so that you need to intialize those properties in your class jsondeserailized and instead of list use cursors because you storing cursors data in paging class .Thanks
How should my C# sharp object look like, I would like to deserialize the following JSON string to a C# object.
{
"PersonId": "XXXXXXXXXXXXXX",
"Name": "XXXXXXXX",
"HobbiesCollection":
{"Hobby":
[
{
"type": "RUNNING",
"id": 44,
"description": "sprinting and sprinting?"
},
{
"type": "RUNNING",
"id": 45,
"description": "jogging and jogging"
}
]
}
}
Here is what is generated in VS2013
public class Rootobject
{
public string PersonId { get; set; }
public string Name { get; set; }
public Hobbiescollection HobbiesCollection { get; set; }
}
public class Hobbiescollection
{
public Hobby[] Hobby { get; set; }
}
public class Hobby
{
public string type { get; set; }
public int id { get; set; }
public string description { get; set; }
}
You can use VS2012/2013 feature Edit/Paste Special/Paste JSON As Classes to simplify this process.
There is a online tool you can make C# class from your String
JSON to Csharp
public class Hobby
{
public string type { get; set; }
public int id { get; set; }
public string description { get; set; }
}
public class HobbiesCollection
{
public List<Hobby> Hobby { get; set; }
}
public class RootObject
{
public string PersonId { get; set; }
public string Name { get; set; }
public HobbiesCollection HobbiesCollection { get; set; }
}
I get some json code from web services in Windows Phone 8. I generate my entities class thanks to the site json2csharp (http://json2csharp.com/). But there is a web service that has strange json code, like this. There is numbering as keys (0,1,2):
{
"service": "MainMapService",
"func": "getPlacesByAxes",
"result": {
"0": {
"id": "13478",
"date": "0",
"id_cat": "1",
"id_cat_sub": "0",
"id_user": "0",
},
"2": {
"id": "23272",
"date": "0",
"id_cat": "1",
"id_cat_sub": "0",
"id_user": "667"
},
"1": {
"id": "21473",
"date": "0",
"id_cat": "1",
"id_cat_sub": "0",
"id_user": "0"
}
},
"args": [
"1",
"50.8",
"4.5",
"1"
]
}
And json2csharp generates classes like this... Each class for a number:
public class __invalid_type__0
{
public string id { get; set; }
public string date { get; set; }
public string id_cat { get; set; }
public string id_cat_sub { get; set; }
public string id_user { get; set; }
}
public class __invalid_type__2
{
public string id { get; set; }
public string date { get; set; }
public string id_cat { get; set; }
public string id_cat_sub { get; set; }
public string id_user { get; set; }
}
public class __invalid_type__1
{
public string id { get; set; }
public string date { get; set; }
public string id_cat { get; set; }
public string id_cat_sub { get; set; }
public string id_user { get; set; }
}
public class Result
{
public __invalid_type__0 __invalid_name__0 { get; set; }
public __invalid_type__2 __invalid_name__2 { get; set; }
public __invalid_type__1 __invalid_name__1 { get; set; }
}
public class RootObject
{
public string service { get; set; }
public string func { get; set; }
public Result result { get; set; }
public List<string> args { get; set; }
}
So, the problem comes from the numbering keys and there may be several numbers. Do you know how can I resolve this? I can't change the Json code...
Thank you in advance
This is far from elegant, but give it a try.
So, what is my idea:
I have created two classes
RootObject helper
public class YourJsonClass
{
public string service { get; set; }
public string func { get; set; }
public dynamic result { get; set; }
public string[] args { get; set; }
}
result helper
public class Item
{
public string id { get; set; }
public string date { get; set; }
public string id_cat { get; set; }
public string id_cat_sub { get; set; }
public string id_user { get; set; }
}
I'm using newtonsoft json
var m_res = JsonConvert.DeserializeObject<YourJsonClass>(YourJsonResponce);
foreach (dynamic numb in m_res.result)
{
string m_name = numb.Name; // it will be "1", "0" or whatever
string h = numb.Value.ToString();
var m_value = JsonConvert.DeserializeObject<Item>(h);
}
... indeed there are better ways, but i hope this will help (: