Deserialize JSON value without name

天大地大妈咪最大 提交于 2019-12-11 06:13:10

问题


How can I deserialize a string in C# that only have values and no name. It looks like this: The problem is that this stream of string does not have name and uses array.

{
  "result": {
    "14400": [
      [
        1502985600,
        262.18,
        262.18,
        257,
        257,
        1096.0131
      ],
      [
        1503000000,
        257,
        261.33,
        254.8,
        257,
        1130.5897
      ]
   ],
 "14405": [
      [
        1503014400,
        258.03,
        261.5,
        257.01,
        257.01,
        520.7805
      ],
      [
        1503028800,
        258,
        260.98,
        252.4,
        259.56,
        298.5658
      ],
   ]
  ]
 }
}

回答1:


Just create a class like

public class Root
{
    public Dictionary<int,List<List<double>>> Result { get; set; }
}

and deserialize as

var res = JsonConvert.DeserializeObject<Root>(json);



回答2:


I see it's an array, you could create a method to parse the class out of given JArray.

The Jason data

  public void methpod()
    {

string json ="Your jason value " 

        var factory = new Factory();
        var suggest = factory.Create(json);

        Console.WriteLine(suggest);
    }

Make a class as suggested :

public class Factory
{
    public Evan Create(string json)
    {
        var array = JArray.Parse(json);
        string search = array[0].ToString();
        string[] terms = array[1].ToArray().Select(item => item.ToString()).ToArray();

        return new Evan{Search = search, Terms = terms};
    }
}

and another

public class Evan
{
    public string Search { get; set; }
    public IEnumerable<string> Terms { get; set; }
    public override string ToString()
    {
        return string.Format("Search={0},Terms=[{1}]", 
            Search, string.Join(",", Terms));
    }
}


来源:https://stackoverflow.com/questions/46103849/deserialize-json-value-without-name

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!