c# 解析json{ "isSuccess":true, "result":true, "code":0, "errCode":0, "message":"失败错误说明", "data":null, "commit"=false }
时间: 2024-01-08 14:04:01 浏览: 125
C#解析json
5星 · 资源好评率100%
以下是使用C#解析JSON的示例代码:
```csharp
using System;
using Newtonsoft.Json;
public class Response
{
public bool isSuccess { get; set; }
public bool result { get; set; }
public int code { get; set; }
public int errCode { get; set; }
public string message { get; set; }
public object data { get; set; }
public bool commit { get; set; }
}
public class Program
{
public static void Main()
{
string json = @"{
""isSuccess"": true,
""result"": true,
""code"": 0,
""errCode"": 0,
""message"": ""失败错误说明"",
""data"": null,
""commit"": false
}";
Response response = JsonConvert.DeserializeObject<Response>(json);
Console.WriteLine("isSuccess: " + response.isSuccess);
Console.WriteLine("result: " + response.result);
Console.WriteLine("code: " + response.code);
Console.WriteLine("errCode: " + response.errCode);
Console.WriteLine("message: " + response.message);
Console.WriteLine("data: " + response.data);
Console.WriteLine("commit: " + response.commit);
}
}
```
这段代码使用了Newtonsoft.Json库来解析JSON字符串。首先,我们定义了一个Response类,该类的属性与JSON中的字段对应。然后,使用JsonConvert.DeserializeObject方法将JSON字符串转换为Response对象。最后,我们可以通过访问Response对象的属性来获取JSON中的值。
阅读全文