org.json で JSON を解析する [重複] 質問する

org.json で JSON を解析する [重複] 質問する

次のようなサーバーからの出力を解析しようとしています:

{
  "GetFolderFilesByZoneResult": [
    {
      "ID": 98748,
      "CreatedBy": "",
      "UpdatedBy": "none",
      "CreatedDate": "\/Date(1308273033620+0100)\/",
      "UpdatedDate": "\/Date(1308303003770+0100)\/",  
      "CommentCount": 0,
      "Key": "",
      "Enabled": true,
      "MimeType": "video",
      "Votes": 2,
      "TotalRating": 0,
      "AllowComments": true,
      "ViewCount": 323,
      "ReleaseDate": "\/Date(1308273000000+0100)\/",
      "ExpireDate": "\/Date(4102444800000+0000)\/",
      "Author": "",
      "Size": 133799936,
      "Tag1": "",
      "Tag2": "",
      "Tag3": "",
      "RecycleBin": false
    },
    {
      "ID": 99107,
      "CreatedBy": "",
      "UpdatedBy": "none",
      "CreatedDate": "\/Date(1308583412520+0100)\/",
      "UpdatedDate": "\/Date(1308583564007+0100)\/",     
      "CommentCount": 0,
      "Key": "",
      "Enabled": true,
      "MimeType": "video",
      "Votes": 0,
      "TotalRating": 0,
      "AllowComments": true,
      "ViewCount": 33,
      "ReleaseDate": "\/Date(1308583380000+0100)\/",
      "ExpireDate": "\/Date(4102444800000+0000)\/",
      "Author": "",
      "Size": 47955968,
      "Tag1": "",
      "Tag2": "",
      "Tag3": "",
      "RecycleBin": false
    }
  ]
}

Java org.json を使用して解析しようとしていますが、JSON/org.json の経験がないため、少し困っています。これを解析するにはどうすればよいでしょうか?

ベストアンサー1

1) パス上に JSON ライブラリ (www.json.org から) があると仮定すると、非常に簡単です。

import org.json.JSONTokener;
...

URI uri = new URI("http://someserver/data.json");
JSONTokener tokener = new JSONTokener(uri.toURL().openStream());
JSONObject root = new JSONObject(tokener);

そこから、JSON オブジェクトのさまざまな部分を扱うことができます。詳細については、Javadoc を参照してください。出典: android.com

おすすめ記事