从json反序列化后获取字典内容

发布于 2024-11-29 07:22:25 字数 3440 浏览 4 评论 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 并获取字段。

I finally got this to work using the javascriptserializer to deserialize the json content. Now I have the objects in a dictionary and would like to access the contents to get the key values. I want to grab the values of certain fields only.

{
  "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 

code to deserialize;

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

how do I now get the id, from, name, id, to ...etc fields?
Any ideas please.

{updated}

appreciate the response. While I was waiting I tried something else and found what seems to be an easier method if I can only get some notes or examples on this.

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

Ok this seems much easier but I do not know how to traverse the token object. The message field is not read because the type is still Object I guess and I was wondering how can I go through this individual JToken and grab the fields.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

不醒的梦 2024-12-06 07:22:25

反序列化根据数据创建指定的对象。因此,您可以使用如下代码(仅限 .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)

Deserialization creates the specified object based on the data. So, with what you have you could use code like the following (.Net 4.0 only, and this is C# since I don't know VB):

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

and so on.

If you don't have the dynamic keyword you will have to use reflection, and that's a long answer.

However, it would be easier if you cast it to the original object type that you used to create that JSON. Or, if this is coming from an external source, create new types in your source code that map to that data tree, and deserialize to that.

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

class Bar {
    string name;
    string id;
}

...

Then deserialize to (VB):

 Dim ddict as Dictionary(of String, Foo) = dserial.Deserialize(Of Dictionary(Of string, Foo))(jsonData)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文