0

I am trying to fetch testId of the item having name Item2 from following JSON file:

{
  "errors": [],
  "data": {
    "collections": [
      {
        "testId": 250,
        "name": "Item1",
        "itemCount": 3
      },

      {
        "testId": 350,
        "name": "Item2",
        "itemCount": 4
      },
      {
        "testId": 411,
        "name": "Item3",
        "itemCount": 1
      },
      {
        "testId": 450,
        "name": "null",
        "itemCount": 0
      }
    ]
  }
}

This JSON is response of GET request I ran on postman.

I can fetch value of any key in this JSON but I am not able to add relative conditions like fetching testID of an item with name Item6.

I'm using Array list to fetch values of testID for all elements, which is working fine for me. Can I make any change in the following to achieve what I want?

protected String  getTokenValueUnderHierarchy( String responseString) throws ClientProtocolException, IOException {
        String responseRecordsTitle = null;
        List<String> list = Arrays.asList(responseString.split("testId"));
        System.out.println("list.size()::"+list.size());
        for (int i=0;i<list.size();i++){        
            System.out.println("List Element:"+list.get(i));
            responseRecordsTitle = getTokenValue(responseString,"testId");
        }
        return responseRecordsTitle;

    }

PS: getTokenValue(String,String) is a function which I've created to fetch value and it works fine.

SSabharwal
  • 159
  • 12
  • Is there any specific reason to write custom logic? why can't you use existing api like shown in this url https://www.mkyong.com/java/jackson-2-convert-java-object-to-from-json/ Always better to convert json to object for easy maintenance( helps in future) – RamPrakash Jan 05 '17 at 21:52
  • Yes! I need this logic because ID I will fetch, is going to be used as URL parameter for other service request. – SSabharwal Jan 06 '17 at 13:40

2 Answers2

0

You can user JSON parser available for java. Here is one of them How to parse JSON in Java. When you retrieve values from the JSON array you can do a simple string comparison to check for the conditions.! Hope that helps.

Community
  • 1
  • 1
0

I Got it! Thanks for the tip @Castor. Following worked for me:

responseJson is the JSON file I'm getting as response from GET request.

protected int getIdFromArrayOfCollectionItems(String responseJson) throws JSONException{
    int collectionId=0;
    JSONObject root = new JSONObject(responseJson).getJSONObject("data");
    JSONArray collectionArray = root.getJSONArray("collections");
    for(int i =0 ;i<collectionArray.length();i++){
        JSONObject collectionObject = collectionArray.getJSONObject(i);
        String name = collectionObject.getString("name"); 
        if(name.equalsIgnoreCase("Item2")){
            collectionId = collectionObject.getInt("collectionId"); 
            System.out.println("collection ID::"+collectionId);
        }
    }
    return collectionId;

}

Of course I will pass Item name as argument for more dynamic behavior, but it works :)

SSabharwal
  • 159
  • 12