简体   繁体   中英

How do i split a string of jsonObjects into an array

I'm getting some data from the Trello API over HTTP. So an example of the response would be:

'[{"name":"asd","desc":"yes"},{"name":"xyz","desc":"no"}]'

I'm using the volley library for making the request and getting the response. Is there a way for me to get the response in the form of json objects directly instead of in a string?

If not how should I proceed?

Thanks!

You can use JSONArray() . And then you can use getString() so you can use all string function.

Example code:

JSONArray jsonArray = new JSONArray(responseString);
int i = 0;
while (i <jsonArray.length()) {
    JSONObject jsonObj = jsonArray.getJSONObject(i);
    String name = jsonObj.getString("name");
    String description = jsonObj.getString("desc");
    //TODO create your Java object and store these strings into it.
    i++;
}

use volley can solve easily.

JsonArrayRequest jsonArrayRequest = new JsonArrayRequest(url,new Response.Listener<JSONArray>(){

        @Override
        public void onResponse(JSONArray response) {
             //the response is JsonArray       
        }
    },new Response.ErrorListener(){

        @Override
        public void onErrorResponse(VolleyError error) {

        }
});

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