-1

I need the count the number of specific array (coreid) present in response JSON Since, response I'm getting is pretty huge, I am using following JSON as example:

 {
    "errors": [],
    "data": {
        "products": {
            "number": 0,
            "size": 2,
            "total": 2,
            "results": [{
                    "assetId": "29-test-ENG",
                    "fields": {
                        "majoracademic": [
                            "test stream 1"
                        ],
                        "learningobjective": [
                            "test objective 1"
                        ],
                        "hasproductprice": [
                            "0"
                        ],

                        "comingsoon": [
                            "0"
                        ],

                        "coreid": [
                            "12"
                        ],
                        "previewCount": [
                            "0"
                        ],
                        "type": [
                            "page(s)"
                        ]
                    }
                },

                {
                    "assetId": "01-test-MTH",
                    "fields": {
                        "majoracademic": [
                            "test stream 2"
                        ],
                        "learningobjective": [
                            "test objective 2"
                        ],
                        "hasproductprice": [
                            "0"
                        ],

                        "comingsoon": [
                            "0"
                        ],

                        "coreid": [
                            "12"
                        ],
                        "previewCount": [
                            "0"
                        ],
                        "type": [
                            "page(s)"
                        ]
                    }
                }
            ]
        }
    }
 }

So Basically I need to count array "coreid" occurrence in this JSON, which is actually 2 but my code is returning 3 times, I'm using the following code:

protected String  getTokenValueUnderHierarchyString( String Json) throws ClientProtocolException, IOException {
    List<String> list = Arrays.asList(Json.split("coreid"));
    System.out.println("list.size()::"+list.size());

}

I printed the values of array, turns out it takes complete JSON object as 1st element. So even If I type "coreid1, array count it will return is 1. and the value would be complete JSON.

I hope my question is making any sense.

SSabharwal
  • 159
  • 12

1 Answers1

1

Split is not meant to count occurences. It split (hence the name ;) ) a string at a given separator. You have three because you have some text before the first coreid some text between the two and some text after the last.

Also, your solution will break if the string coreid is present anywhere else. A better approach would be to parse the JSON and then analyse it to find the number of key that are coreid.

Community
  • 1
  • 1
litelite
  • 2,857
  • 4
  • 23
  • 33
  • 1
    totally agree as far as last part of answer is concerned, if there is another field like scoreid, then it will be included in coreid search even though it wasn't meant to. – Rahul Kumar Jan 18 '17 at 18:29
  • Parsing JSON was my first call, it did not work for me (probably some petty mistake), that is why I had to jump on lists and array. But "countMatches" function helped me to find the count at least.It is not an optimal solution, but temporarily it worked. I will try parsing again – SSabharwal Jan 18 '17 at 19:05