0

I'm trying to process a JSON structure with Json.NET but without any luck.

{
  "Test": [
    {
      "text": "blah...",
      "Id": 6,
      "Date": "2013-04-13T00:00:00"
    },
    {
      "text": "bluuhh...",
      "Id": 7,
      "Date": "2013-02-10T00:00:00"
    }
  ],
  "ErrorCode": 0,
  "Status": 0,
  "StatusString": "Okay",
  "Message": "successfully returned 2 events."
}

Usually I write:

dynamic stuff = JsonConvert.DeserializeObject(json);

How is it possible to make a foreach for text?

4

2 回答 2

3
dynamic stuff = JsonConvert.DeserializeObject(json);
foreach (var item in stuff.Test)
{
    Console.WriteLine("{0} {1} {2}", item.text, item.Id, item.Date);
}
于 2013-09-10T13:02:37.887 回答
2

一种方法是通过自动生成器json2csharp从 json 创建对象

它给你的 json

public class Test
{
    public string text { get; set; }
    public int Id { get; set; }
    public string Date { get; set; }
}

public class RootObject
{
    public List<Test> Test { get; set; }
    public int ErrorCode { get; set; }
    public int Status { get; set; }
    public string StatusString { get; set; }
    public string Message { get; set; }
}

那么你有

RootObject stuff = JsonConvert.DeserializeObject<RootObject>(json);
foreach (Test item in stuff.Test)
{
    //your code
}
于 2013-09-10T13:04:09.453 回答