Java 使用 org.json 解析 JSON

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6442347/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-16 05:57:15  来源:igfitidea点击:

Parse JSON with org.json

javajsonorg.json

提问by Paul Ulrich

I'm trying to parse an output from a server that looks like this:

我正在尝试解析来自服务器的输出,如下所示:

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

I'm trying to use Java org.json to parse it, but I don't have any experience with JSON/org.json, so I'm having a little trouble. How can I parse this?

我正在尝试使用 Java org.json 来解析它,但是我对 JSON/org.json 没有任何经验,所以我遇到了一些麻烦。我该如何解析?

采纳答案by JohnnyO

1) Assuming you have the JSON libraries on your path (from www.json.org), it's pretty easy.

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);

From there, you can address the various parts of the JSON object. Take a look at the Javadocs for the specifics. https://developer.android.com/reference/org/json/package-summary.html

从那里,您可以处理 JSON 对象的各个部分。请查看 Javadoc 以了解详细信息。 https://developer.android.com/reference/org/json/package-summary.html

回答by TravMan

I'd pass it

我会通过的

map<String, Object> 

loaded with

满载

map<String, Object> 

in the object field.

在对象领域。

Basically recreating the hierarchy of your java classes inside of a large map.

基本上是在大地图中重新创建 Java 类的层次结构。

Example :

例子 :

return ( Map<"GetFolderFilesByZoneResult", Map<"Result1", (object by id 98748) | "Result2", (object by id 99107) | "Result3", etc.

JSON will return that big map very pretty like, and programmatically it's easier to do then lists.

JSON 将返回非常漂亮的大地图,并且以编程方式比列表更容易执行。

回答by Nevkontakte

Here is the most universal solution, which allows to parse any JSON type into appropriate Java type:

这是最通用的解决方案,它允许将任何 JSON 类型解析为适当的 Java 类型:

Object json = new JSONTokener(response).nextValue();

Then you can determine resulting type and handle it appropriately.

然后您可以确定结果类型并适当处理它。