1

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!

7
  • 1
    one minute googling for: volley json developer.android.com/training/volley/request.html#request-json Commented Oct 9, 2015 at 11:12
  • 1
    i could not really understand your question , but in Spring we create a model class with the fields "name" and "desc" with setter and getter. Ant We can use annotation to indicate to Spring to map json to the model. I dont know if it has helped but if u want i can give more information. Commented Oct 9, 2015 at 11:12
  • 1
    What strings? "name", "asd", "desc", "yes"? Please elaborate what you want to do. Commented Oct 9, 2015 at 11:13
  • 1
    Why dont you just parse the string in json array and get the json objects from it. Commented Oct 9, 2015 at 11:15
  • 1
    @guy You should mark the answer by alican. Commented Oct 9, 2015 at 11:25

2 Answers 2

4

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++;
}
Sign up to request clarification or add additional context in comments.

2 Comments

Sufian thanks:) JSONObject reader = new JSONObject(responseString); String res = reader.getString("TAG"); Like JSONArray.
Thank you :) It is better.
2

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

        }
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.