c# - 在 C# 中解析 JSON 对象

标签 c# json

我使用 JSON.NET我想解析从 WebService 获得的以下对象。有人可以发布有关如何执行此操作的示例吗?

@"{""MessageType"":0,
   ""Message"":""Success"",
   ""Value"":[
              {""listId"":1,
               ""listName"":""DemoList"",
               ""itemInList"":[
                    {
                     ""fromDate"":""\/Date(1228946400000)\/"",
                     ""fromLocation"":null,
                     ""toLocation"":null,
                     ""originalRequest"":""water"",
                     ""creationDate"":""\/Date(1339448400000)\/"",
                     ""typeId"":1
                    },
                    {
                     ""fromDate"":null,
                     ""fromLocation"":null,
                     ""toLocation"":null,
                     ""originalRequest"":""gala"",
                     ""creationDate"":""\/Date(1304370000000)\/"",
                     ""typeId"":1
                    }
              ]}
    ]}"

JSON 对象

{
  "MessageType":0,
  "Message":"UserLists",
  "Value":
          [
            {
              "listId":1,
              "listName":"DemoList",
              "itemInList" 
                    [
                      {
                         "fromDate":"\/Date(1228946400000)\/",
                         "fromLocation":null,
                         "toLocation":null,
                         "originalRequest":"water",
                         "creationDate":"\/Date(1339448400000)\/",
                         "typeId":1
                      },
                      {
                         "fromDate":null,
                         "fromLocation":null,
                         "toLocation":null,
                         "originalRequest":"gala",
                         "creationDate":"\/Date(1304370000000)\/",
                         "typeId":1
                       }
                  ],
                  "numberOfItems":2
              }
          ]
     }

谢谢。

最佳答案

您需要像这样创建一些实体:

public class Entity
{
    public int MessageType { get; set; }
    public string Message { get; set; }
    public List<EntityValue> Value { get; set; }
}

public class EntityValue
{
    public int listId { get; set; }
    public string listName { get; set; }
    public List<ItemInList> itemInList { get; set; }
}

public class ItemInList
{
    public DateTime? fromDate { get; set; }
    public string fromLocation { get; set; }
    public string toLocation { get; set; }
    public string originalRequest { get; set; }
    public DateTime creationDate { get; set; }
    public int typeId { get; set; }
}

实体必须具有与 json 数据相同的结构。 您可以调用该方法:

JsonConvert.DeserializeObject<Entity>(json);

如果有任何异常,需要调整实体直到它起作用。

关于c# - 在 C# 中解析 JSON 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9963079/

相关文章:

wcf - 如何在 WCF REST 中调整默认的 JSON 序列化程序

java - 使用 Jackson 2 将 JSON 反序列化为 Java

json - 如何在 react 表单元格中包含网址

使用 ExcelDNA 的 C# 程序集

c# - 如何在 C# 中通过字符串调用构造函数(即通过反射)?

javascript - 解析 JSON 时出现问题

java - 使用 Apache 异步 http 客户端进行 Http POST + 身份验证

c# - add-windowsfeature 无法识别

c# - 无法将 User32.dll 导入 Visual Studio

c# - 如何从sql日期中删除时间