0

我终于使用 javascriptserializer 来反序列化 json 内容。现在我在字典中有对象,并且想访问内容以获取键值。我只想获取某些字段的值。

{
  "data": [
  {
     "id": "56381779049_10150352298839050",
     "from": {
        "name": "Paulina Soto",
        "id": "1619854594"
     },
     "to": {
        "data": [
           {
              "name": "Pepsi",
              "category": "Food/beverages",
              "id": "56381779049"
           }
        ]
     },
     "message": "La coca es mejor que la pepsi :D.",
     "type": "status",
     "created_time": "2011-08-11T20:43:18+0000",
     "updated_time": "2011-08-11T20:43:18+0000",
     "comments": {
        "count": 0
     }
  },
  {
     "id": "56381779049_10150352296084050",
     "from": {
        "name": "William Scott Jennings",
        "id": "1125852789"
     },
     "to": {
        "data": [
           {
              "name": "Pepsi",
              "category": "Food/beverages",
              "id": "56381779049"
           }
        ]
     },
     "message": "Don't buy the new Pepsi can coming out with pictures of the Empire State building and The Pledge of Allegiance on them. Pepsi left out two little words on the pledge, \"Under God.\" Pepsi said they didn't want to offend anyone. So if we don't buy them they won't be offended when they don't receive our money that has the words \"In God We Trust\" on it. How fast can you re post this? It is offensive to leave out Under God\r\nDon't buy the new Pepsi can coming out with pictures of the Empire State building and The Pledge of Allegiance on them. Pepsi left out two little words on the pledge, \"Under God.\" Pepsi said they didn't want to offend anyone. So if we don't buy them they won't be offended when they don't receive our money that has the words \"In God We Trust\" on it. How fast can you re post this? It is offensive to leave out Under God\r\n",
     "type": "status",
     "created_time": "2011-08-11T20:39:59+0000",
     "updated_time": "2011-08-11T20:39:59+0000",
     "comments": {
        "count": 0
     }
  },
  {
     "id": "56381779049_10150352295939050",
     "from": {
        "name": "William Scott Jennings",
        "id": "1125852789"
     },
     "to": {
        "data": [
           {
              "name": "Pepsi",
              "category": "Food/beverages",
              "id": "56381779049"
           }
        ]
     },
     "message": "Don't buy the new Pepsi can coming out with pictures of the Empire State building and The Pledge of Allegiance on them. Pepsi left out two little words on the pledge, \"Under God.\" Pepsi said they didn't want to offend anyone. So if we don't buy them they won't be offended when they don't receive our money that has the words \"In 

反序列化代码;

dim dserial as new javascriptSerializer()
Dim ddict as Dictionary(of String, Object) = dserial.Deserialize(Of Dictionary(Of string, object))(jsonData)

我现在如何获取 id、from、name、id、to ...等字段?请有任何想法。

{更新}

感谢回复。当我在等待时,我尝试了其他方法,发现如果我只能获得一些关于此的注释或示例,似乎是一种更简单的方法。

dim results as list(of JToken) = jobj.("data").Children().ToList()
for each item as Jtoken in results
  uid = item("id")
  if item.type = JTokenType.Object then
      author = item.SelectToken("from.name")
      authorId = item.SelectToken("from.id")
  end if
  msg = item("message")

 next

好的,这似乎更容易,但我不知道如何遍历令牌对象。未读取消息字段,因为我猜该类型仍然是 Object ,我想知道如何通过这个单独的 JToken 并获取字段。

4

1 回答 1

1

反序列化根据数据创建指定的对象。所以,有了你所拥有的,你可以使用如下代码(仅限.Net 4.0,这是 C#,因为我不知道 VB):

dynamic data = ddict["data"]; 
string foo = data[0].from.id; 
string bar = data[1].to.data[0].name == "Pepsi"

等等。

如果您没有动态关键字,则必须使用反射,这是一个很长的答案。

但是,如果将其转换为用于创建该 JSON 的原始对象类型会更容易。或者,如果这来自外部源,请在源代码中创建映射到该数据树的新类型,然后反序列化为该数据树。

class Foo {
    string id;
    Bar from;
    ...
}

class Bar {
    string name;
    string id;
}

...

然后反序列化为(VB):

 Dim ddict as Dictionary(of String, Foo) = dserial.Deserialize(Of Dictionary(Of string, Foo))(jsonData)
于 2011-08-11T20:53:32.097 回答