2016-09-30 127 views
0

我已经在这个格式的奇怪配制JSON响应字符串:C#非标准JSON解析

{ 
    "Result": <this is the array of Ticket objects>, 
    "IsLastPage": true, 
    "NextSkip": 1, 
    "NextTake": 1, 
    "PageCount": 2, 
    "TotalCount": 3, 
    "QueryResultHash": "sample string 4" 
} 

一般我将访问JSON数组(上面的结果的值)当阵列是唯一被返回,像这样:

var jsonArray = JArray.Parse(resultString); 
foreach (var jsonObject in jsonArray) 
{ ... } 

但我不知道如何打破上面的字符串,这样我可以分别获得7个值,并解析阵列。有什么建议么?

+0

你的意思是说你通常将数组作为json中唯一的项目吗? –

+1

这是什么JSON实际上看起来像? I.E'<这是Ticket对象的数组>'是实际值吗? – Darren

+0

不,数组是一个正确的公式字符串,如:{“Id”:4412,“Rev”:30,“Fields”:{“System”...我的意思通常和过去一样,当我上次使用JSON –

回答

1

如果可能,我会使用库Newtonsoft.Json(https://www.nuget.org/packages/Newtonsoft.Json/)。

然后你可以创建一个ResponseContainer类。喜欢的东西,

//generated by http://json2csharp.com/ 
public class ResponseContainer 
{ 
    public List<object> Result { get; set; } 
    public bool IsLastPage { get; set; } 
    public int NextSkip { get; set; } 
    public int NextTake { get; set; } 
    public int PageCount { get; set; } 
    public int TotalCount { get; set; } 
    public string QueryResultHash { get; set; } 
} 

然后,你可以做

JsonSerializer serializer = new JsonSerializer(); 
ResponseContainer response = serializer.Deserialize<ResponseContainer>(jsonString); 

现在,您可以访问JSON响应为C#对象的字段。

+1

您可以使用本网站[json2csharp](http://json2csharp.com/)了解您的模型或容器将看起来的样子 –

+0

尼斯,这是一个非常方便的工具。我将编辑生成的代码。 –