处理 JSONObject 以获取项



此代码:

JSONObject jsonObj= JSONObject.fromString(request.getParameter("parameter"));
System.out.println(jsonObj.getString("dList"));

给了我一个字符串:

["ABC","CGR","DRR"]

如何处理它以获取值?尝试像JSONArray一样,

JSONArray arr= jsonObj.getJSONArray("dList");

但显示以下错误。

JSONObject["dList"] is not a JSONArray.

以下是原始数据:

 {"sort":null,
 "filterFunction":null,
 "source":["ABC","CGR","DRR"],
 "length":3,
 "source":["ABC","CGR","DRR"],
 "length":3}}

我正在通过dList作为Flex JSON.encode(dList.source).

To get the jsonobject in the JSON array
     JSONObject object = jsonarray.getJSONObject(index);

To get the values from JSON object
Json values should be Key and value pair format 
    JSONObject myJson = new JSONObject(myJsonString);
    String name = myJson.get("name");
**Example**
 String s = "{menu:{"1":"sql", "2":"android", "3":"mvc"}}";
    JSONObject jObject  = new JSONObject(s);
    JSONObject  menu = jObject.getJSONObject("menu");
    Map<String,String> map = new HashMap<String,String>();
    Iterator iter = menu.keys();
    while(iter.hasNext()){
        String key = (String)iter.next();
        String value = menu.getString(key);
        map.put(key,value);
    }

试试这个:

JSONObject jsonObj = new JSONObject(request.getParameter("parameter"));
JSONArray array = jsonObj.getJSONArray("dList");
for (int i = 0; i < array.length(); i++) {
    System.out.println(array.get(i));
}

如果您使用此 Jar 文件,这有效

尝试:

JSONObject jsonObj= JSONObject.fromString(request.getParameter("parameter"));  // will get  parameter in JSONObject
JSONArray arr= jsonObj.getJSONArray("dList"); // will get dList from jsonObj and stored to JSONArray
for( int i = 0 ; i < arr.length() ; i++ ){ //  will iterate up to length of array
    try { // for to handle JSON Exception
         JSONObject object = arr.getJSONObject(i); // will get ith object into JSONObject from array
         System.out.println(object.toString()); // print object
    } catch (JSONException e) {
         e.printStackTrace();
    }
}

查看运行示例:

 public static void main(String[] args) throws JSONException {
        JSONObject jo = new JSONObject();
        jo.put("language", "Java");
        JSONArray ja = new JSONArray();
        ja.put(jo);
        for( int i = 0 ; i < ja.length() ; i++ ){
             JSONObject object = ja.getJSONObject(i);
             System.out.println(object.getString("language"));
        }   
 }

这将打印Java作为输出。

不推荐,但如果您的数据将固定为 3 个字符,则正在运行:

(使用字符串操作)

public static void main(String[] args) throws JSONException {
        JSONArray ja = new JSONArray();
        String[] myStringArray = {"ABC","CGR","DRR"};
        JSONObject j = new JSONObject();
        j.put("source", myStringArray);
        ja.put(j);
        for( int i = 0 ; i < ja.length() ; i++ ){
            JSONObject object = ja.getJSONObject(i);
            if(object.has("source")){
                String a = object.toString();
                String b = a.substring(a.indexOf("[") + 1, a.lastIndexOf("]"));
                List<String> strArr = Arrays.asList(b.split("","));
                for(int j1=0; j1<strArr.size();j1++){
                    if(strArr.get(j1).length() == 4){ System.out.println(strArr.get(j1).substring(strArr.get(j1).indexOf(""")+1));
                    }else if (strArr.get(j1).length() == 5){ System.out.println(strArr.get(j1).substring(strArr.get(j1).indexOf(""")+1,strArr.get(j1).lastIndexOf(""") ));
                    }else { System.out.println(strArr.get(j1)); }
                }
            }
        }
    }

相关内容

  • 没有找到相关文章

最新更新