如何获得对json.net数组的访问?

时间:2011-07-03 00:38:01

标签: c# silverlight parsing windows-phone-7 json.net

{
  "response": [
    50961,
    {
      "mid": 148014,
      "date": 1309642994,
      "out": 0,
      "uid": 39050402,
      "read_state": 1,
      "title": " ... ",
      "body": "здоров"
    },
    {
      "mid": 148012,
      "date": 1309641130,
      "out": 0,
      "uid": 56015000,
      "read_state": 1,
      "title": "...",
      "body": ")"
    },
    {
      "mid": 148010,
      "date": 1309639892,
      "out": 0,
      "uid": 56015000,
      "read_state": 1,
      "title": " ... ",
      "body": "Привет)"
    },

我如何解析它:

string json = e.Result;
var response = JObject.Parse(json);
Debug.WriteLine(response);
var messages_in = new iVk.App.MessagesInStructure[count_messages];
for (int i = 0; i < count_messages; i++)
{
    messages_in[i].readstate = Convert.ToBoolean(response["response"][i]["read_state"]);
    messages_in[i].text = response["response"][i]["body"].ToString();
    messages_in[i].otpravitel = response["response"][i]["uid"].ToString();
    messages_in[i].date_time = Convert.ToDateTime(response["response"][0][i]["date"]);
}

但是它没有任何作用。我如何能够访问[响应] [日期]和其他人? 我有错误“无法访问Newtonsoft.Json.Linq.JValue上的子值。”

1 个答案:

答案 0 :(得分:2)

这是错误的:

Convert.ToDateTime(response["response"][0][i]["date"]);

response [“response”] [0]是一个整数,因为响应[“response”]中的第一个元素是“50961”,所以响应[“response”] [0] [i]将失败,因为你试图索引不是数组的东西