0

我目前使用以下代码来检索我的 Json 字符串中第一条记录的值,

string JsonString = "{'response':[{'bigINT':123456789,'smallINT':12345},{'bigINT':00000000,'smallINT':00000},{'bigINT':999999999,'smallINT':99999}]}";
JObject Jobj = JObject.Parse(JsonString);

int firstbigINT = (int)Jobj["response"][0]["bigINT"];      // 123456789
int firstsmallINT = (int)Jobj["response"][0]["smallINT"];  // 12345

这很好用,但是我希望使用 foreach 来遍历所有记录,例如-

foreach (string record in Jobj)
{
    int bigINT = (int)Jobj["response"][0]["bigINT"];    
    int smallINT = (int)Jobj["response"][0]["smallINT"];

    use(bigINT,smallINT)
    // then go to next record 
}

因为我需要这两个值。

我尝试使用 -

JsonTextReader reader = new JsonTextReader(new StringReader(JsonString));

while (reader.Read())
{
    Console.WriteLine(reader.TokenType + " - " + reader.ValueType + " - " + reader.Value)
}

但这会一个接一个地拆分这些值。

4

2 回答 2

0

这不起作用吗?

string JsonString = "{'response':[{'bigINT':123456789,'smallINT':12345},{'bigINT':00000000,'smallINT':00000},{'bigINT':999999999,'smallINT':99999}]}";
        JObject Jobj = JObject.Parse(JsonString);

        foreach (var response in Jobj["response"]) 
        {
          int firstbigINT = (int)response["bigINT"];
          int firstsmallINT = (int)response["smallINT"];
          use(bigINT,smallINT)
        }
于 2013-10-10T11:54:32.733 回答
0

试试这个方法

object obj = Newtonsoft.Json.JsonConvert.DeserializeObject<List<YourClassName>>(json .ToString());

这应该工作

于 2013-10-10T12:00:57.927 回答