-2

I'm developing an Android application, I have to implement a function that parse my JSON and return to me certain result.

For example, I have a JSON with this strcutures:

[  
   {  
      "date":"17-06-2016",
      "desc":"My description",
      "id":"9",
      "img":"http:\/\/myurl\/pathimage\/image.jpg",
      "text":"Lorem Ipsum.",
      "title":"My title"
   },   {  
      "date":"14-06-2015",
      "desc":"My description 2",
      "id":"5",
      "img":"http:\/\/myurl\/pathimage\/image.jpg",
      "text":"Lorem Ipsum 2.",
      "title":"My title 2"
   }

]

I need to select ONLY the object with id = 9, how i can do this ? How i can select programmatically the JSON object discriminating respect the id ?

I know how to parse JSON, but i DON'T know how to select programmatically the JSON object discriminating respect the id.

****** EDIT 1 ******

JSONArray newsReq;
        try {
            newsReq = new JSONArray(result.toString());
            for (int i = 0; i < newsReq.length(); i++) {
                try {
                  if(obj.getInt("id")=='9'){
                    JSONObject obj = newsReq.getJSONObject(i);
                    obj.getString("img");
                    obj.getInt("id");
                    obj.getString("title");
                 }
                } catch (JSONException e) {
                    Log.wtf("BulgariLog: ", e);
                }
            }
        } catch (JSONException e) {
            Log.wtf("BulgariLog: ", e);
        }
Mattia
  • 21
  • 6
  • 2
    Iterate over all objects compare the ID. It's not possible to select only that ID – OneCricketeer Jun 17 '16 at 14:44
  • You can't. As @cricket_007 pointed out, your only option is to iterate over your objects testing the `id` parameter until you find `9`. Think about it for a moment. How else would you know if a value is "9" unless you actually read the value first? – NoChinDeluxe Jun 17 '16 at 14:47
  • You could try your luck with [JsonPath](https://github.com/jayway/JsonPath), no guarantees, though – OneCricketeer Jun 17 '16 at 14:49
  • What happend with "programmers" now? ... "implement function which would find and return an item with given predicate in collection" was a basic excercise back then – Selvin Jun 17 '16 at 14:50
  • 1
    `obj.getString("id")=='9'` that is not how we do compare strings in java – Selvin Jun 17 '16 at 14:52
  • well, ["9".equals('9') is false](http://ideone.com/O3XTQc) '9' is char "9" is string – Selvin Jun 17 '16 at 14:54

1 Answers1

0

Assuming you are pasring the JSON array like this,

JSONArray jsonArray = new JSONArray(response);

        for (int i = 0; i < jsonArray.size(); i++) {

            JSONObject jsonObject = jsonArray.getJSONObject(i);

            // get the id of current JSON object
            String id = jsonObject.getString("id");

            // check if the id matches with your required id
            if (id.contains("9")) {
                // the JSON object matches with your desired one
                // get the remaining values here
            } else {
            // skip the object since it doesn't matches your given id 
            }
        }
Prokash Sarkar
  • 11,723
  • 1
  • 37
  • 50