在Java中解析Json如何阅读内在价值

时间:2013-05-24 08:24:30

标签: java json

Java Development解析JSON JAVA Code这是我的package com.zenga.control; import java.io.BufferedReader; import org.apache.commons.httpclient.HttpClient; import org.apache.commons.httpclient.methods.GetMethod; import net.sf.json.JSONArray; import net.sf.json.JSONObject; import net.sf.json.JSONSerializer; public class Start { public String readUrl(String urlString) { String jsonString = null; try { HttpClient client = new HttpClient(); GetMethod get = new GetMethod(urlString); client.executeMethod(get); jsonString = get.getResponseBodyAsString(); }catch(Exception e) { } return jsonString; } public void getAddAsBeanObject() { try { String jsonString = new Start().readUrl("http://myDomain/JsonZ.json"); JSONObject obj = (JSONObject) JSONSerializer.toJSON(jsonString); System.out.println(obj); } catch (Exception e) { e.printStackTrace(); } } public static void main(String[] args) { new Start().getAddAsBeanObject(); } }

JSONObject

当我在JSON中成功读取值并且它还在控制台上显示所有ID字符串但我如何获得UIDDURATION以及{{1}的价值}? 这是我在JSON

中读取的System.out.println(obj);字符串
{
  "Demo": {
    "CONTENT": [
      {
        "ID": " 283 ",
        "UID": " 87897bc8-ae9b-11e1-bdcf-123141042154 ",
        "DURATION": "Full"
      },
      {
       "ID": " 283 ",
        "UID": " 87897bc8-ae9b-11e1-bdcf-123141042154 ",
        "DURATION": "Full"
      }
    ]
  }
}

6 个答案:

答案 0 :(得分:1)

以下是一个示例代码,用于访问特定于您提供的模式的数组内部对象。

String str = "{"+
      "\"Demo\": {"+
        "\"CONTENT\": ["+
         " {"+
        "\"ID\": \" 283 \","+
        "\"UID\": \" 87897bc8-ae9b-11e1-bdcf-123141042154 \","+
        "\"DURATION\": \"Full\""+
         " },"+
          "{"+
        "\"ID\": \" 283 \","+
        "\"UID\": \" 87897bc8-ae9b-11e1-bdcf-123141042154 \","+
        "\"DURATION\": \"Full\""+
         " }"+
        "]"+
      "}"+
    "}";

try {
    JSONObject jsr = new JSONObject(str); // JSON object with above data
    JSONObject demo = jsr.getJSONObject("Demo"); // get Demo which is a JSON object inside jsr.
    JSONArray content = demo.getJSONArray("CONTENT");// get CONTENT which is Json array inside Demo
    for (int i = 0; i < content.length(); i++) { // iterate over array to get inner JSON objects and extract values inside
        JSONObject record = content.getJSONObject(i); // each item of Array is a JSON object
        String ID = record.getString("ID");
        String UID = record.getString("UID");
        String DURATION = record.getString("DURATION");
    }
}
catch (JSONException e) {
    e.printStackTrace();
} 

注意:上面的代码是org.Json API的特定代码。在用于Json处理的库中找到适当的方法

答案 1 :(得分:0)

使用循环遍历Json对象并访问每个元素

for(/**loop until the counter reaches the size of the json object**/)  {

    //Access each element based on the ID as below.

    System.out.println(Demo.CONTENT[CurrentCounter].ID); //here CurrentCounter is index
    System.out.println(Demo.CONTENT[CurrentCounter].UID); ..... //read through all ids

} 

答案 2 :(得分:0)

我猜你可以在JSONObject上使用'get'方法。如果你不知道要查找哪个键,我建议使用一个返回所有可用键的方法,比如名为'keys'的键。使用这些值,您可以在您的结构中向下遍历。看这里: http://json-lib.sourceforge.net/apidocs/net/sf/json/JSONObject.html

答案 3 :(得分:0)

我猜GSON对你有很大的帮助。 见Parsing json object into a string

还有一个样本代码

答案 4 :(得分:0)

以下代码可用于迭代 JSON数组'CONTENT'中的JSON对象,使用记录的.get(java.lang.String)将值拉出JSONObject

我只演示了如何获取ID,但同样的逻辑适用于其他值。

JSONObject obj = (JSONObject) JSONSerializer.toJSON(jsonString);
JSONArray content = obj.getJSONObject("Demo").getJSONArray("CONTENT");

java.util.Iterator<?> iterator = content.iterator();
while (iterator.hasNext()) {
    JSONObject o = (JSONObject) iterator.next();
    System.out.println(o);
    System.out.println(o.get("ID"));
    // etc...
}

答案 5 :(得分:-1)

创建一个包含要从json string读取的变量的类.Gson将处理其余的。

https://code.google.com/p/google-gson/

使用示例:

//convert the json string back to object
DataObject obj = gson.fromJson(br, DataObject.class);