简体   繁体   中英

C# JsonConvert - List of Lists with Multiple Types

So I'm trying to parse a JSON request that looks a bit like this:

{
"source_code":"MOODY",
"code":"DAAAYLD",
"name":"Aaa Corporate Bond Yield",
"data":[
    [
        "2015-08-11",
        3.96
    ],
    [
        "2015-08-10",
        4.06
    ],
    [
        "2015-08-07",
        3.96
    ]
]
}

There's a few more items I've left out but they're not important. Now, I'm using

var jsonData = JsonConvert.DeserializeObject<JsonResponseObj>(GetRawData(code));

to make a JsonResponseObj which looks like:

public class JsonResponseObj
    {
        [JsonProperty("name")]
        public string name { get; set; }

        [JsonProperty("code")]
        public string code { get; set; }

        [JsonProperty("source_code")]
        public string source_code { get; set; }

        [JsonProperty("description")]
        public string description { get; set; }

        [JsonProperty("data")]
        public List<List<string>> data { get; set; }
    }

This works fine for getting the name, codes etc. if I omit the "data" property, however because the data itself is a list of lists (or list of two-tuples if you want to look at it that way) I'm not quite sure how the correct way of fetching this is. I've tried a few things which all seem to break the parser, including the most obvious one to me of:

[JsonProperty("data")]
public List<Tuple<string, float>> data { get; set; }

However that just gives an error of:

Cannot deserialize the current JSON array (eg [1,2,3]) into type 'System.Tuple`2[System.String,System.Single]' because the type requires a JSON object (eg {"name":"value"}) to deserialize correctly.

But the actual type isn't a JSON object, as it has no Key/Value pairs, just 2 values for each type of "string, float".

Any clues on how this would be best parsed to get the data?

Okay, so I've discovered dynamic types which do away with having a seperate static object for the JSON which didn't work with nested list strings.

Basically I solved it with one line of code:

dynamic jsonData = JsonConvert.DeserializeObject<dynamic>(rawJsonStr);

Which means I can then call on the top-level values directly, and use a JArray to iterate through the "data" list as below:

string name = jsonData.name;
string data_code = jsonData.code;
string source_code = jsonData.source_code;
JArray data = jsonData.data;
foreach (var point in data)
{
    System.Diagnostics.Debug.WriteLine(point[0] + " | " + point[1]);    
}

The problem exists because of data it's not List>, because each entry contains string and double element so i've a quick solution for this issue just implement your class as the following.

public class JsonResponseObj { [JsonProperty("name")] public string name { get; set; }

    [JsonProperty("code")]
    public string code { get; set; }

    [JsonProperty("source_code")]
    public string source_code { get; set; }

    [JsonProperty("description")]
    public string description { get; set; }

    [JsonProperty("data")]
    public List<Dictionary<string,double>> data { get; set; }
}
{
  "source_code": "MOODY",
  "code": "DAAAYLD",
  "name": "Aaa Corporate Bond Yield",
  "data": [
    {
      "2015-08-11": 3.96
    },
    {
      "2015-08-11": 3.96
    },
    {
      "2015-08-11": 3.96
    }
  ]
}

**I config syntax .json, like this, you will use List<Dictonary<string,float> data ** like u want

public class Root
{
    public string source_code { get; set; }
    public string code { get; set; }
    public string name { get; set; }
    public List<Dictionary<string,float>> data { get; set; }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM