在Android中未正确解析和拆分JSON

在Android中未正确解析和拆分JSON,android,json,Android,Json,我试图解析一个包含未知数据的JSON数组。JSON的JSON就是这样通过的 { "info": [ { "1": "4", "Store #": " 0560", "How many microwave circuits did you run?": " 3", "How many new ovens did you deliver to the store?": " 1",

我试图解析一个包含未知数据的
JSON数组。JSON的
JSON
就是这样通过的

{
    "info": [
        {
            "1": "4",
            "Store #": " 0560",
            "How many microwave circuits did you run?": " 3",
            "How many new ovens did you deliver to the store?": " 1",
            "How many new racks did you deliver to the store?": " 5",
            "Voltage readings on Turbo Chef 1": " 64",
            "Voltage readings on Turbo Chef 2": " 54",
            ...
        }
    ],
    "success": 1
}
try {
// Checking for SUCCESS TAG
int success = json.getInt(TAG_SUCCESS);

if (success == 1) {
info = json.getJSONArray(TAG_INFO);
    for (int i = 0; i < info.length(); i++) {
        if (info != null) {             
            clientList.add(info.get(i).toString());             
        }
    }
    for (String s : clientList) {
        Log.v("CHECKING S", s);
        String[] str_arr= s.split("[:,]");                                              
        Log.v("CHECKING VALUES 0", str_arr[0]);
        mQuestions.add(str_arr[0]);
        Log.v("CHECKING VALUES 1", str_arr[1]);
    }
    for (String content : mQuestions) {
        Log.v("QUESTIONS", content);
    }

}
现在,因为在收到数据之前我不知道数据是什么,所以我不能使用传统的方法,比如

for (int i = 0; i < info.length(); i++) {

    JSONObject c = info.getJSONObject(i);

    String store = c.getString("Store #");
}
那么我做错了什么?如果你还需要密码,我会把它贴出来。谢谢你的帮助

编辑

根据我收到的答案,我对代码进行了修改。现在看起来像这样

{
    "info": [
        {
            "1": "4",
            "Store #": " 0560",
            "How many microwave circuits did you run?": " 3",
            "How many new ovens did you deliver to the store?": " 1",
            "How many new racks did you deliver to the store?": " 5",
            "Voltage readings on Turbo Chef 1": " 64",
            "Voltage readings on Turbo Chef 2": " 54",
            ...
        }
    ],
    "success": 1
}
try {
// Checking for SUCCESS TAG
int success = json.getInt(TAG_SUCCESS);

if (success == 1) {
info = json.getJSONArray(TAG_INFO);
    for (int i = 0; i < info.length(); i++) {
        if (info != null) {             
            clientList.add(info.get(i).toString());             
        }
    }
    for (String s : clientList) {
        Log.v("CHECKING S", s);
        String[] str_arr= s.split("[:,]");                                              
        Log.v("CHECKING VALUES 0", str_arr[0]);
        mQuestions.add(str_arr[0]);
        Log.v("CHECKING VALUES 1", str_arr[1]);
    }
    for (String content : mQuestions) {
        Log.v("QUESTIONS", content);
    }

}

您需要将
s.split(“:”)
结果分配给字符串数组,如下所示:

for (String s : clientList) {
             Log.v("CHECKING S", s);
             String[] str_arr= s.split(":"); //<<<<<
             Log.v("CHECKING S SPLIT", str_arr);

             Log.v("CHECKING VALUES 0", str_arr[0]);
             mQuestions.add(str_arr[0]);
             Log.v("CHECKING VALUES 1", str_arr[1]);
             mAnswers.add(str_arr[1]);                        
        }
for(字符串s:clientList){
Log.v(“检查S”,S);

String[]str_arr=s.split(“:”);//您需要将
s.split(“:”)
结果分配给字符串数组,如下所示:

for (String s : clientList) {
             Log.v("CHECKING S", s);
             String[] str_arr= s.split(":"); //<<<<<
             Log.v("CHECKING S SPLIT", str_arr);

             Log.v("CHECKING VALUES 0", str_arr[0]);
             mQuestions.add(str_arr[0]);
             Log.v("CHECKING VALUES 1", str_arr[1]);
             mAnswers.add(str_arr[1]);                        
        }
for(字符串s:clientList){
Log.v(“检查S”,S);

String[]str_arr=s.split(“:”);//您好,我已经为您完成了这项工作。我只是在粘贴下面的完整代码。尝试这种json,它肯定会对您有用

我将该方法称为:

String data = "{'info': [{'1': '4','Store #': ' 0560','How many microwave circuits did you run?': ' 3','How many new ovens did you deliver to the store?': ' 1','How many new racks did you deliver to the store?': ' 5','Voltage readings on Turbo Chef 1': ' 64','Voltage readings on Turbo Chef 2': ' 54'}],'success': 1}";

    try {
        JSONObject jsonData = new JSONObject(data);
        populateFromResponse(jsonData);

        } catch (JSONException e) {
            System.out.println(" Exception occured" + e.getMessage());
             e.printStackTrace();
        }
    }
现在,populateFromResponse()方法的实现如下所示:

private static final String TAG_SUCCESS = "success"; private static final String TAG_INFO = "info"; private ArrayList<JSONObject> clientList; private ArrayList<String> mQuestions; private ArrayList<String> mAnswers;

  public void populateFromResponse(JSONObject json) throws JSONException {

    // parse the response   if (json != null) {

        // Checking for SUCCESS TAG         int success = json.getInt(TAG_SUCCESS);         // checking for null        if (success == 1) {             JSONArray info = json.getJSONArray(TAG_INFO);           if (info != null) {
                for (int i = 0; i < info.length(); i++) {
                    // checking for null
                    if (info.get(i) != null
                            && info.get(i) instanceof JSONObject)
                        if (clientList == null) {
                            clientList = new ArrayList<JSONObject>();
                        }
                    clientList.add(info.getJSONObject(i));
                }           }

            for (JSONObject s : clientList) {
                Log.v("CHECKING S", s.toString());
                // Iterator containing all the keys
                Iterator<String> iterator = s.keys();
                while (iterator.hasNext()) {
                    String key = (String) iterator.next();
                    String value = s.getString(key);

                    print("CHECKING VALUES 0: " + key);
                    // checking for null
                    if (mQuestions == null) {
                        mQuestions = new ArrayList<String>();
                    }
                    mQuestions.add(key);

                    print("CHECKING VALUES 1: " + value);
                    // checking for null
                    if (mAnswers == null) {
                        mAnswers = new ArrayList<String>();
                    }
                    mAnswers.add(value);
                }

                print("CHECKING S SPLIT: " + s);

            }       }   } else {        print("response is null");  }

}

private void print(String string) {     System.out.println("######-- " + string + " -----#########"); }
现在享受代码并快乐起来。
再见。

您好,我已经为您完成了这项工作。我只是在粘贴下面的完整代码。尝试一下这种json,它肯定会对您有用

我将该方法称为:

String data = "{'info': [{'1': '4','Store #': ' 0560','How many microwave circuits did you run?': ' 3','How many new ovens did you deliver to the store?': ' 1','How many new racks did you deliver to the store?': ' 5','Voltage readings on Turbo Chef 1': ' 64','Voltage readings on Turbo Chef 2': ' 54'}],'success': 1}";

    try {
        JSONObject jsonData = new JSONObject(data);
        populateFromResponse(jsonData);

        } catch (JSONException e) {
            System.out.println(" Exception occured" + e.getMessage());
             e.printStackTrace();
        }
    }
现在,populateFromResponse()方法的实现如下所示:

private static final String TAG_SUCCESS = "success"; private static final String TAG_INFO = "info"; private ArrayList<JSONObject> clientList; private ArrayList<String> mQuestions; private ArrayList<String> mAnswers;

  public void populateFromResponse(JSONObject json) throws JSONException {

    // parse the response   if (json != null) {

        // Checking for SUCCESS TAG         int success = json.getInt(TAG_SUCCESS);         // checking for null        if (success == 1) {             JSONArray info = json.getJSONArray(TAG_INFO);           if (info != null) {
                for (int i = 0; i < info.length(); i++) {
                    // checking for null
                    if (info.get(i) != null
                            && info.get(i) instanceof JSONObject)
                        if (clientList == null) {
                            clientList = new ArrayList<JSONObject>();
                        }
                    clientList.add(info.getJSONObject(i));
                }           }

            for (JSONObject s : clientList) {
                Log.v("CHECKING S", s.toString());
                // Iterator containing all the keys
                Iterator<String> iterator = s.keys();
                while (iterator.hasNext()) {
                    String key = (String) iterator.next();
                    String value = s.getString(key);

                    print("CHECKING VALUES 0: " + key);
                    // checking for null
                    if (mQuestions == null) {
                        mQuestions = new ArrayList<String>();
                    }
                    mQuestions.add(key);

                    print("CHECKING VALUES 1: " + value);
                    // checking for null
                    if (mAnswers == null) {
                        mAnswers = new ArrayList<String>();
                    }
                    mAnswers.add(value);
                }

                print("CHECKING S SPLIT: " + s);

            }       }   } else {        print("response is null");  }

}

private void print(String string) {     System.out.println("######-- " + string + " -----#########"); }
现在享受代码并快乐起来。
再见。

你在哪里向
值添加项目?未知数据,你的意思是未知值吗?@SmartLemon是的,我永远不知道
JSON
包含什么,或者它将包含多少值。你不知道在什么级别?你知道它是否会有一个信息数组吗?@SmartLemon它将始终具有信息数组,我创建了它,但是res它的t是未知的。此外,信息数组将始终有一个“success”标记它将是1或0,您在哪里向
值添加项目?未知数据,您是指未知值吗?@SmartLemon是的,我永远不知道
JSON
包含什么,或者它将包含多少值。您不知道在什么级别?您知道它是否将具有信息数组吗?@SmartLemon它将始终具有信息数组,我知道吃掉它,但它的其余部分是未知的。此外,信息数组将始终有一个“成功”的标记谢谢你的回答!你是最好的。而且,现在logcat显示我也需要在
上拆分,因为每行
JSON
的结尾都是
谢谢你的回答!你是最好的。另外,现在logcat显示我也需要在
上拆分,因为eac的结尾h
JSON
行以