如何在c#中将上述JSON字符串反序列化为可数据表

本文关键字:字符串 JSON 反序列化 数据表 | 更新日期: 2023-09-27 18:08:09

如何将以下JSON字符串反序列化为c#中的数据表

{
  "m_MaxCapacity":2147483647,
  "Capacity":1888,
  "m_StringValue":"<table border=3><tr><th>Master ID</th><th>Tag ID</th><th>Plant ID</th><th>Machine Name</th><th>Sap ID</th><th>Log</th></tr><tr><td>2296</td><td>567</td><td>567</td><td>hjhnh</td><td>567</td><td>17-09-2016 15:03:04</td></tr><tr><td>2297</td><td>55555</td><td>567</td><td>hjhnh</td><td>567</td><td>17-09-2016 15:04:27</td></tr><tr><td>2298</td><td>55555</td><td>567</td><td>hjhnh</td><td>0000</td><td>17-09-2016 15:04:53</td></tr><tr><td>2299</td><td>55555</td><td>567</td><td>hjhnh</td><td>0000</td><td>17-09-2016 15:05:11</td></tr><tr><td>2300</td><td>6678</td><td>6754</td><td>nnn</td><td>789</td><td>17-09-2016 15:20:51</td></tr><tr><td>2301</td><td>6678</td><td>6754</td><td>AF</td><td>789</td><td>17-09-2016 15:23:57</td></tr><tr><td>2302</td><td>6678</td><td>6754</td><td>ttttttt</td><td>789</td><td>17-09-2016 15:33:22</td></tr><tr><td>2303</td><td>6678</td><td>6754</td><td>ttttttt</td><td>789</td><td>17-09-2016 15:43:10</td></tr><tr><td>2304</td><td>6678</td><td>6754</td><td>ttttttt</td><td>789</td><td>17-09-2016 15:43:23</td></tr><tr><td>2305</td><td>6678</td><td>6754</td><td>ttttttt</td><td>789</td><td>17-09-2016 15:43:50</td></tr><tr><td>2306</td><td>6678</td><td>6754</td><td>lmno</td><td>789</td><td>17-09-2016 15:49:25</td></tr><tr><td>2307</td><td>6678</td><td>6754</td><td>ttttttt</td><td>789</td><td>22-09-2016 11:23:16</td></tr><tr><td>2308</td><td>6678</td><td>6754</td><td>ttttttt</td><td>789</td><td>22-09-2016 11:40:07</td></tr><tr><td>2309</td><td>6678</td><td>6754</td><td>ccccc</td><td>789</td><td>22-09-2016 11:40:18</td></tr><tr><td>2310</td><td>6678</td><td>6754</td><td>ttttttt</td><td>789</td><td>22-09-2016 11:45:53</td></tr><tr><td>2311</td><td>6678</td><td>6754</td><td>ttttttt</td><td>789</td><td>22-09-2016 12:00:48</td></tr><tr><td>2312</td><td>6678</td><td>6754</td><td>mmmmmmmmmm</td><td>789</td><td>22-09-2016 12:00:52</td></tr></table>",
  "m_currentThread":0
}

如何在c#中将上述JSON字符串反序列化为可数据表

首先,您必须将包Newtonsoft.Json添加到您的项目中。接下来用对象创建类。你可以使用http://json2csharp.com/,这是一个非常有用的工具。最后,您必须Deserialize您的json。试着使用这个示例。

 public class RootObject
    {
        public long m_MaxCapacity { get; set; }
        public int Capacity { get; set; }
        public string m_StringValue { get; set; }
        public int m_currentThread { get; set; }
    }
string json=   {"m_MaxCapacity":2147483647,"Capacity":1888,"m_StringValue":"Master IDTag IDPlant IDMachine NameSap IDLog2296567567hjhnh56717-09-2016 15:03:04229755555567hjhnh56717-09-2016 15:04:27229855555567hjhnh000017-09-2016 15:04:53229955555567hjhnh000017-09-2016 15:05:11230066786754nnn78917-09-2016 15:20:51230166786754AF78917-09-2016 15:23:57230266786754ttttttt78917-09-2016 15:33:22230366786754ttttttt78917-09-2016 15:43:10230466786754ttttttt78917-09-2016 15:43:23230566786754ttttttt78917-09-2016 15:43:50230666786754lmno78917-09-2016 15:49:25230766786754ttttttt78922-09-2016 11:23:16230866786754ttttttt78922-09-2016 11:40:07230966786754ccccc78922-09-2016 11:40:18231066786754ttttttt78922-09-2016 11:45:53231166786754ttttttt78922-09-2016 12:00:48231266786754mmmmmmmmmm78922-09-2016 12:00:52","m_currentThread":0}
List<RootObject> list = new List<RootObject>();
list= JsonConvert.DeserializeObject<List<RootObject>>(json);

可以使用:http://json2csharp.com/

你得到了这个类:

public class RootObject
{
    public long m_MaxCapacity { get; set; }
    public int Capacity { get; set; }
    public string m_StringValue { get; set; }
    public int m_currentThread { get; set; }
}

那么你可以把它添加到你的上下文中