简体   繁体   English

如何在JSON文件中不使用数组就循环JSON数据和解析

[英]How to Loop JSON Data & Parse without Array in JSON file

I'm trying to parse JSON data that has no array, at the moment my code loops through a JSONArray and works fine. 我正在尝试解析没有数组的JSON数据,此刻我的代码循环通过JSONArray并正常工作。

ISSUE/ERROR ISSUE / ERROR

I'm not sure how to do the same with an JSON without an array. 我不确定如何在不使用数组的情况下使用JSON。 how do I loop when I don't have an array length to deal with in a JSON with no array. 没有数组长度以处理没有数组的JSON时如何循环

Code: - Currently works for JSON data with array 代码:-当前适用于带有数组的JSON数据

JSONArray inbox = null;

List<NameValuePair> params = new ArrayList<NameValuePair>();

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

try {

        inbox = json.getJSONArray(TAG_ARRAY);
    inbox.toString();

 // looping through All messages
 for (int i = 0; i < inbox.length(); i++) {
 JSONObject c = inbox.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);
….. 
…..
…..

JSON WITHOUT ARRAY JSON不带数组

[
    {
        "id": "1",
        "person": "David",
        "thur": "",
        "grade": "15",
        "round": "",
        "tour": ""
    },
    {
        "id": "T2",
        "person": "Mary",
        "thur": "",
        "grade": "13",
        "round": "",
        "tour": ""
    },


]

JSON WITH ARRAY - Code above works fine for this JSON WITH ARRAY-上面的代码对此适用

{
    "id": [
        {
            "id": "1",
            "person": "Jason",
            "thur": "F",
            "grade": "17",
            "round": "2"
        },
        {
            "id": "2",
            "person": "Joe",
            "grade": "F",
            "score": "16",
           "round": "3"
        }

]

Have you tried this? 你有尝试过吗?

JSONObject jsonObject = new JSONObject(); try { JSONArray jsonArray = new JSONArray(jsonObject.toString()); } catch (JSONException e) { e.printStackTrace(); }

But I advice you to use Google Library GSON ( https://github.com/google/gson ) to get JSON values from rest client 但我建议您使用Google Library GSON( https://github.com/google/gson )从其他客户端获取JSON值

dont know how your parser method (makeHttpRequest()) works but change the defination of the makeHttpRequest() method ..so it return jsonarray.. as your json is of jsonarray..which further contains jsonobject at each index 不知道您的解析器方法(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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM