2

非常新的Java Development解析JSONJAVA这里是我的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在控制台上显示所有字符串但是我怎样才能获得值IDUIDDURATION?这里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"
      }
    ]
  }
}
4

6 回答 6

1

以下代码可用于迭代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...
}
于 2013-05-24T09:01:43.547 回答
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 处理的库中查找适当的方法

于 2013-05-24T08:56:10.477 回答
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

} 
于 2013-05-24T08:34:47.703 回答
0

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

于 2013-05-24T08:35:54.257 回答
0

我想GSON会对你有很大帮助。请参阅此处将 json 对象解析为字符串

还有一个示例代码

于 2013-05-24T08:58:13.030 回答
-1

创建一个类,其中包含要从 json 字符串中读取的变量。Gson 将处理其余部分。

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

示例用法:

//convert the json string back to object
DataObject obj = gson.fromJson(br, DataObject.class);
于 2013-05-24T08:42:53.330 回答