(入门)JSON反序列化给DataTable带来麻烦

时间:2018-11-05 09:17:42

标签: json serialization dataset

我是JSON反序列化对象的始作俑者。我尝试使用JSON.NET。 我有一个向我发送JSON消息的硬件设备。

{
    "settings": [{
        "slotid": 13,
        "live": {
            "values": [
                {"id":1,"v":"3.5.0"},
                {"id":2,"v":""},
                {"id":3,"v":282827},
                {"id":4,"v":127},
                {"id":5,"v":127},
                {"id":10,"v":7},
                {"id":11,"v":1},
                {"id":12,"v":22},
                {"id":13,"v":1},
                {"id":14,"v":1},
                {"id":15,"v":0},        
                {"id":16,"v":2},
                {"id":17,"v":0},
                {"id":18,"v":0}
            ]
        },
        "presets": []
    }]
}

我尝试完全按照

https://www.newtonsoft.com/json/help/html/DeserializeDataSet.htm

但是出了点问题,我想这取决于JSON应答格式的构建。

我使用了这样的代码:

 public class Product
  {
      public string settings {get; set;}
      public string values { get; set; }
      //public string presets { get; set; }
      public string slotid { get; set; }
  }
    private void Form1_Load(object sender, EventArgs e)
    {
        string URL = "http://10.0.0.201/api/json/controls/get?slotid=13";
        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(URL);
        request.ContentType = "application/json; charset=utf-8";
        HttpWebResponse response = request.GetResponse() as HttpWebResponse;
        using (Stream responseStream = response.GetResponseStream())
        {
            StreamReader reader = new StreamReader(responseStream, Encoding.UTF8);
            string json = reader.ReadToEnd();
            try
            {
                Product deserializedProduct = JsonConvert.DeserializeObject<Product>(json);

            }
            catch(Exception E)
            {

            }
        }
    }

在json变量中,我保留JSON消息。

我想我必须建立自定义数据表,但是如何?

致谢

Ps。

我忘了说我只需从值中的JSON中获取“ id”和“ v”。

0 个答案:

没有答案