Chaitanya Nettem
Chaitanya Nettem

Reputation: 1239

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!

Upvotes: 1

Views: 2261

Answers (2)

Egos Zhang
Egos Zhang

Reputation: 1354

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) {

        }
});

Upvotes: 2

alican akyol
alican akyol

Reputation: 288

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++;
}

Upvotes: 4

Related Questions