Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/364.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/213.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何循环JSON数据&;JSON文件中无数组的解析_Java_Android_Arrays_Json_Loops - Fatal编程技术网

Java 如何循环JSON数据&;JSON文件中无数组的解析

Java 如何循环JSON数据&;JSON文件中无数组的解析,java,android,arrays,json,loops,Java,Android,Arrays,Json,Loops,我正在尝试解析没有数组的JSON数据,目前我的代码在JSONArray中循环,工作正常。 问题/错误 我不知道在没有数组的情况下如何处理JSON。在没有数组的JSON中,如果没有数组长度可处理,如何循环。 代码:-当前用于带有数组的JSON数据 上面带有数组代码的JSON可以很好地实现这一点 你试过这个吗 JSONObject JSONObject=new JSONObject(); 试一试{ JSONArray JSONArray=新的JSONArray(jsonObject.toSt

我正在尝试解析没有数组的JSON数据,目前我的代码在JSONArray中循环,工作正常。

问题/错误 我不知道在没有数组的情况下如何处理JSON。在没有数组的JSON中,如果没有数组长度可处理,如何循环。

代码:-当前用于带有数组的JSON数据 上面带有数组代码的JSON可以很好地实现这一点 你试过这个吗

JSONObject JSONObject=new JSONObject();
试一试{
JSONArray JSONArray=新的JSONArray(jsonObject.toString());
}捕获(JSONException e){
e、 printStackTrace();
}

但我建议您使用Google Library GSON()从rest客户端获取JSON值,您尝试过吗

JSONObject JSONObject=new JSONObject();
试一试{
JSONArray JSONArray=新的JSONArray(jsonObject.toString());
}捕获(JSONException e){
e、 printStackTrace();
}


但是我建议您使用googlelibrary GSON()从rest客户端获取JSON值

我不知道您的解析器方法(makeHttpRequest())是如何工作的 但是更改makeHttpRequest()方法的定义..因此它返回jsonarray。。 因为您的json是jsonarray..,它在每个索引处进一步包含jsonobject

JSONArray inbox = null;
List<NameValuePair> params = new ArrayList<NameValuePair>();

// getting JSON string from URL
JSONArray json = jsonParser.makeHttpRequest(INBOX_URL, "GET",params);

try {
    //inbox = json.getJSONArray(TAG_ARRAY);
    //inbox.toString();

    //comment the above 2 line and the rest is same
    // looping through All messages
    for (int i = 0; i < json.length(); i++) {
    JSONObject c = json.getJSONObject(i);

    // Storing each json item in variable
    String id = c.getString(TAG_ID);
    String person = c.getString(TAG_PERSON);
    // creating new HashMap
    HashMap<String, String> map = new HashMap<String, String>();

    // adding each child node to HashMap key => value
    map.put(TAG_ID, id);
    map.put(TAG_FROM, person);
JSONArray收件箱=null;
List params=new ArrayList();
//从URL获取JSON字符串
JSONArray json=jsonParser.makeHttpRequest(INBOX_URL,“GET”,params);
试一试{
//inbox=json.getJSONArray(标记数组);
//inbox.toString();
//对以上两行进行注释,其余内容相同
//循环浏览所有消息
for(int i=0;ivalue
地图放置(标签标识,标识);
地图放置(标签从,人);

不知道解析器方法(makeHttpRequest())是如何工作的 但是更改makeHttpRequest()方法的定义..因此它返回jsonarray。。 因为您的json是jsonarray..,它在每个索引处进一步包含jsonobject

JSONArray inbox = null;
List<NameValuePair> params = new ArrayList<NameValuePair>();

// getting JSON string from URL
JSONArray json = jsonParser.makeHttpRequest(INBOX_URL, "GET",params);

try {
    //inbox = json.getJSONArray(TAG_ARRAY);
    //inbox.toString();

    //comment the above 2 line and the rest is same
    // looping through All messages
    for (int i = 0; i < json.length(); i++) {
    JSONObject c = json.getJSONObject(i);

    // Storing each json item in variable
    String id = c.getString(TAG_ID);
    String person = c.getString(TAG_PERSON);
    // creating new HashMap
    HashMap<String, String> map = new HashMap<String, String>();

    // adding each child node to HashMap key => value
    map.put(TAG_ID, id);
    map.put(TAG_FROM, person);
JSONArray收件箱=null;
List params=new ArrayList();
//从URL获取JSON字符串
JSONArray json=jsonParser.makeHttpRequest(INBOX_URL,“GET”,params);
试一试{
//inbox=json.getJSONArray(标记数组);
//inbox.toString();
//对以上两行进行注释,其余内容相同
//循环浏览所有消息
for(int i=0;ivalue
地图放置(标签标识,标识);
地图放置(标签从,人);

您需要更改
makeHttpRequest
以便它可以返回JSONArray。您需要更改
makeHttpRequest
以便它可以返回JSONArray。
{
    "id": [
        {
            "id": "1",
            "person": "Jason",
            "thur": "F",
            "grade": "17",
            "round": "2"
        },
        {
            "id": "2",
            "person": "Joe",
            "grade": "F",
            "score": "16",
           "round": "3"
        }

]
JSONArray inbox = null;
List<NameValuePair> params = new ArrayList<NameValuePair>();

// getting JSON string from URL
JSONArray json = jsonParser.makeHttpRequest(INBOX_URL, "GET",params);

try {
    //inbox = json.getJSONArray(TAG_ARRAY);
    //inbox.toString();

    //comment the above 2 line and the rest is same
    // looping through All messages
    for (int i = 0; i < json.length(); i++) {
    JSONObject c = json.getJSONObject(i);

    // Storing each json item in variable
    String id = c.getString(TAG_ID);
    String person = c.getString(TAG_PERSON);
    // creating new HashMap
    HashMap<String, String> map = new HashMap<String, String>();

    // adding each child node to HashMap key => value
    map.put(TAG_ID, id);
    map.put(TAG_FROM, person);