Reputation: 25
Okay so I've been stuck with JSON.NET before and I'm stuck again, thanks to my last question you guys helped me and I successfully completed my project :3. However now I need to deserialize a piece of JSON that doesn't really have a key. Well it does but its an integer and it can vary between 0 - 50 which means I have no idea on how to do this. I tried using an IDictionary but that failed miserably... Anyways heres the JSON:
{
"response": {
"success": 1,
"current_time": 1362339098, // this will return the time when cache was generated
"prices": {
"35": { // defindex
"11": { // quality
"0": { // price index ( crate # or unusual effect, 0 when not used )
"current": { // current price
"currency": "keys",
"value": 39,
"value_high": 41,
"date": 1357515306 // date when the price was updated
},
"previous": { // previous price
"currency": "keys",
"value": 37,
"value_high": 39
}
}
},
"3": { // quality
"0": { // price index
"current": {
"currency": "metal",
"value": 0.33,
"value_high": 0.66
}
}
}
},
"5002": { // refined metal, useful for converting usd prices into metal
"6": {
"0": {
"current": {
"currency": "usd",
"value": 0.39,
"value_high": 0.42,
"date": 1358090106
}
}
}
},
"5022": { // one of the crate defindex
"6": {
"1": { // crate #1
"current": {
"currency": "metal",
"value": 1.33,
"value_high": 1.55,
"date": 1357515175
}
}
}
}
}
}
}
(Dat formatting... Again...)
And heres my pathetic attempt:
public class Json1 {
public Json2 response { get; set; }
}
public class Json2 {
public int success { get; set; }
public string current_time { get; set; }
public IDictionary<int, Json3> prices { get; set; }
}
public class Json3 {
}
public class Json4 {
}
public class Json5 {
public Json6 current { get; set; }
}
public class Json6 {
public string currency { get; set; }
public string value { get; set; }
public string value_high { get; set; }
}
The Json 3 and 4 are empty because I keep deleting to try different things...
But yeah... I am getting used to json but can't figure this one out. Any friendly responses are appreciated greatly in advance.
(I know I used strings on some floats and longs, that was on purpose but I suppose can be changed)
Upvotes: 0
Views: 5732
Reputation: 129767
You were on the right track with IDictionary
. This JSON structure is actually a bunch of nested dictionaries. Try making your classes like this:
public class Json1
{
public Json2 response { get; set; }
}
public class Json2
{
public int success { get; set; }
public string current_time { get; set; }
public IDictionary<int, IDictionary<int, IDictionary<int, Json5>>> prices { get; set; }
}
public class Json5
{
public Json6 current { get; set; }
}
public class Json6
{
public string currency { get; set; }
public string value { get; set; }
public string value_high { get; set; }
}
You can deserialize it like this:
Json1 obj = JsonConvert.DeserializeObject<Json1>(json);
Once you've deserialized it you can get at the values like this:
foreach (KeyValuePair<int, IDictionary<int, IDictionary<int, Json5>>> price in obj.response.prices)
{
Console.WriteLine("price index: " + price.Key);
foreach (KeyValuePair<int, IDictionary<int, Json5>> quality in price.Value)
{
Console.WriteLine("\t quality: " + quality.Key);
foreach (KeyValuePair<int, Json5> index in quality.Value)
{
Console.WriteLine("\t\t index: " + index.Key);
Console.WriteLine("\t\t\t currency: " + index.Value.current.currency);
Console.WriteLine("\t\t\t value: " + index.Value.current.value);
Console.WriteLine("\t\t\t value_high: " + index.Value.current.value_high);
}
}
}
Output of the above (to show that it works):
price index: 35
quality: 11
index: 0
currency: keys
value: 39
value_high: 41
quality: 3
index: 0
currency: metal
value: 0.33
value_high: 0.66
price index: 5002
quality: 6
index: 0
currency: usd
value: 0.39
value_high: 0.42
price index: 5022
quality: 6
index: 1
currency: metal
value: 1.33
value_high: 1.55
Upvotes: 2
Reputation: 2085
If you have visual studio 2012 you can do Edit -> Paste Special -> Paste JSON as Classes which generates this with your JSON. Although in this case that probably won't help you since the Property Names are dynamic. Maybe the structure helps though. Another option might be to parse the response into a JToken and then use linq to get data out.
public class Rootobject
{
public Response response { get; set; }
}
public class Response
{
public int success { get; set; }
public int current_time { get; set; }
public _35 _35 { get; set; }
public _5002 _5002 { get; set; }
public _5022 _5022 { get; set; }
}
public class _35
{
public _11 _11 { get; set; }
public _3 _3 { get; set; }
}
public class _11
{
public _0 _0 { get; set; }
}
public class _0
{
public Current current { get; set; }
public Previous previous { get; set; }
}
public class Current
{
public string currency { get; set; }
public int value { get; set; }
public int value_high { get; set; }
public int date { get; set; }
}
public class Previous
{
public string currency { get; set; }
public int value { get; set; }
public int value_high { get; set; }
}
public class _3
{
public _01 _0 { get; set; }
}
public class _01
{
public Current1 current { get; set; }
}
public class Current1
{
public string currency { get; set; }
public float value { get; set; }
public float value_high { get; set; }
}
public class _5002
{
public _6 _6 { get; set; }
}
public class _6
{
public _02 _0 { get; set; }
}
public class _02
{
public Current2 current { get; set; }
}
public class Current2
{
public string currency { get; set; }
public float value { get; set; }
public float value_high { get; set; }
public int date { get; set; }
}
public class _5022
{
public _61 _6 { get; set; }
}
public class _61
{
public _1 _1 { get; set; }
}
public class _1
{
public Current3 current { get; set; }
}
public class Current3
{
public string currency { get; set; }
public float value { get; set; }
public float value_high { get; set; }
public int date { get; set; }
}
Upvotes: 1
Reputation: 99
I personnally use Newtonsoft.Json is very nice, you can find it at http://json.codeplex.com/ it handle anonymous type and linq between many other stuff.
there is a good documentation also. si you should be ok :
just a quick example :
serialize :
var listId = new List<int>();
listId.Add(1);
listId.Add(2);
listId.Add(3);
String jsonList = JsonConvert.SerializeObject(listId);
deserialize :
List<int> listID = JsonConvert.DeserializeObject<List<int>>(JsonListOfID);
Upvotes: 2