Z
zydjohn
Guest
Hello:
I had a C# code to parse some json data with rather wired structure. One Json data is like this:
Let’s say its name is: root_json_data:
root_json_data:
{"1": {"id": 1, "symbol": [{"id": 1, "name": "A", "bids": [{"price": 2.0, "amount": 100.0}], "asks": [{"price": 3.0, "amount": 100.0}]}]}} ,
{"2": {"id": 2, "symbol": [{"id": 2, "name": "B", "bids": [{"price": 2.8, "amount": 200.0}], "asks": [{"price": 3.2, "amount": 200.0}]}]}}
The above root_json_data can be parsed into the following 2 child json_data:
json_data1:
{"1": {"id": 1, "history": [], "symbol": [{"id": 1, "name": "A", "last_price": 0, "bids": [{"price": 2.0, "amount": 100.0}], "asks": [{"price": 3.0, "amount": 100.0}]}]}}
json_data2:
{"2": {"id": 2, "history": [], "symbol": [{"id": 2, "name": "B", "last_price": 0, "bids": [{"price": 2.8, "amount": 200.0}], "asks": [{"price": 3.2, "amount": 200.0}]}]}}
However, trying to use a C# class to express any of the Json data is not so easy, the following is an example trying to express json_data2:
public class RootObject
{
[JsonProperty("2")]
public The2 The2 { get; set; }
}
public partial class The2
{
[JsonProperty("id")]
public long Id { get; set; }
[JsonProperty("symbol")]
public Symbol[] Symbol { get; set; }
}
public partial class Symbol
{
[JsonProperty("id")]
public long Id { get; set; }
[JsonProperty("name")]
public string Name { get; set; }
[JsonProperty("bids")]
public Ask[] Bids { get; set; }
[JsonProperty("asks")]
public Ask[] Asks { get; set; }
}
public partial class Ask
{
[JsonProperty("price")]
public double Price { get; set; }
[JsonProperty("amount")]
public long Amount { get; set; }
}
}
Using Newtonsoft.json, I had the following C# code trying to parse the Json data.
It has to parse the children of the root object and going to 2 layers down, then I can parse the data.
The C# code example is something like this:
JToken jtoken_root_data = JToken.Parse(root_json_data);
foreach (JToken level1_down in jtoken_root_data.Children())
{
foreach (JToken json_data in level1_down)
{
RootObject json_data1 = json_data.ToObject<RootObject>();
}
}
Now, since C# has built-in Json support, I want to port my previous code using Newtonsoft.Json to use System.Text.Json to do the same job.
I can’t figure it out, it seems rather complicated.
PS: The actual data structure is way more complicate than my data example here, but using the same code can work.
I am now using Visual Studio 2019 Version 16.3.1 on Windows 10 (Version 1903).
Please advice!
Thanks,
Continue reading...
I had a C# code to parse some json data with rather wired structure. One Json data is like this:
Let’s say its name is: root_json_data:
root_json_data:
{"1": {"id": 1, "symbol": [{"id": 1, "name": "A", "bids": [{"price": 2.0, "amount": 100.0}], "asks": [{"price": 3.0, "amount": 100.0}]}]}} ,
{"2": {"id": 2, "symbol": [{"id": 2, "name": "B", "bids": [{"price": 2.8, "amount": 200.0}], "asks": [{"price": 3.2, "amount": 200.0}]}]}}
The above root_json_data can be parsed into the following 2 child json_data:
json_data1:
{"1": {"id": 1, "history": [], "symbol": [{"id": 1, "name": "A", "last_price": 0, "bids": [{"price": 2.0, "amount": 100.0}], "asks": [{"price": 3.0, "amount": 100.0}]}]}}
json_data2:
{"2": {"id": 2, "history": [], "symbol": [{"id": 2, "name": "B", "last_price": 0, "bids": [{"price": 2.8, "amount": 200.0}], "asks": [{"price": 3.2, "amount": 200.0}]}]}}
However, trying to use a C# class to express any of the Json data is not so easy, the following is an example trying to express json_data2:
public class RootObject
{
[JsonProperty("2")]
public The2 The2 { get; set; }
}
public partial class The2
{
[JsonProperty("id")]
public long Id { get; set; }
[JsonProperty("symbol")]
public Symbol[] Symbol { get; set; }
}
public partial class Symbol
{
[JsonProperty("id")]
public long Id { get; set; }
[JsonProperty("name")]
public string Name { get; set; }
[JsonProperty("bids")]
public Ask[] Bids { get; set; }
[JsonProperty("asks")]
public Ask[] Asks { get; set; }
}
public partial class Ask
{
[JsonProperty("price")]
public double Price { get; set; }
[JsonProperty("amount")]
public long Amount { get; set; }
}
}
Using Newtonsoft.json, I had the following C# code trying to parse the Json data.
It has to parse the children of the root object and going to 2 layers down, then I can parse the data.
The C# code example is something like this:
JToken jtoken_root_data = JToken.Parse(root_json_data);
foreach (JToken level1_down in jtoken_root_data.Children())
{
foreach (JToken json_data in level1_down)
{
RootObject json_data1 = json_data.ToObject<RootObject>();
}
}
Now, since C# has built-in Json support, I want to port my previous code using Newtonsoft.Json to use System.Text.Json to do the same job.
I can’t figure it out, it seems rather complicated.
PS: The actual data structure is way more complicate than my data example here, but using the same code can work.
I am now using Visual Studio 2019 Version 16.3.1 on Windows 10 (Version 1903).
Please advice!
Thanks,
Continue reading...