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 ...etc フィールドを取得するにはどうすればよいですか? アイデアをください。

{更新しました}

応答に感謝します。待っている間に別のことを試してみたところ、これに関するメモや例しか得られない場合は、より簡単な方法と思われる方法を見つけました。

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

これははるかに簡単に思えますが、トークン オブジェクトをトラバースする方法がわかりません。タイプはまだオブジェクトであるため、メッセージフィールドは読み取られません。この個々の JToken をどのように調べてフィールドを取得できるか疑問に思っていました。

4

1 に答える 1

1

逆シリアル化は、データに基づいて指定されたオブジェクトを作成します。したがって、次のようなコードを使用できます (.Net 4.0 のみ。これは VB を知らないため、C# です)。

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

等々。

dynamic キーワードがない場合は、リフレクションを使用する必要があります。これは長い答えです。

ただし、その 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 に答える