简体   繁体   中英

Parsing json code to a string in android using volley

i need help to parse this json code to actual strings using android volley. this is the json code: [{"name":"Tayo","0":"Tayo","thread_name":"Welcome","1":"Welcome","post":"Hi there,","2":"Hi there,","post_time":"Sunday","3":"Sunday"},{"name":"Pete","0":"Pete","thread_name":"Welcome","1":"Welcome","post":"Hi,am pete","2":"Hi,am pete","post_time":"Monday","3":"Monday"}].

I have tried other helps but not working. Thanks!

Remember:

  • if the .json content starts with { is considered as a Json Object.

  • if the .json content starts with [ is considered as a Json Array.

so you hava a JsonArray then you can parse your content like this way:

//Obtain the JsonArray
JSONArray jsonArray = new JSONArray(myJsonContent);
// Get the JsonObjects inside JsonArray
for(int i=0;i<jsonArray.length();i++){                        
   JSONObject jsonobject = jsonArray.getJSONObject(i);     
}

// strData is the json data received.

JSONArray jsonArray = new JSONArray(strData);

    for (int i=0; i<jsonArray.length();i++){
        JSONObject jsonObject = jsonArray.getJSONObject(i);

        String name = jsonObject.optString("name");
        String zero = jsonObject.optString("0");
        String thread_name = jsonObject.optString("thread_name");
        String one = jsonObject.optString("1");
        String post = jsonObject.optString("post");
        String two = jsonObject.optString("2");
        String post_time = jsonObject.optString("post_time");
        String three = jsonObject.optString("3");

       //Just an example
       arrayName.add(jsonObject.optString("name"));
    }

You can even use array to store data instead of the string.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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