Search code examples
javaswingadobe-analytics

Can anyone please suggest me how to calculate percentage for certain JSON data Object in java?


responses:

{
    "report":{
        "type":"ranked",
        "elements":[
            {
                "id":"browser",
                "name":"Browser"
            },
            {
                "id":"operatingSystem",
                "name":"Operating System"
            }
        ],
        "reportSuite":{
            "id":"xyz",
            "name":"xyz | .com"
        },
        "period":"October 2015",
        "metrics":[
            {
                "id":"revenue",
                "name":"Revenue",
                "type":"currency",
                "decimals":2,
                "latency":xxxx,
                "current":false
            }
        ],
        "data":[
            {
                "name":"Google Chrome 45.0",
                "url":"",
                "counts":[
                    "450000.41"
                ],
                "breakdown":[
                    {
                        "name":"Windows 7",
                        "url":"",
                        "counts":[
                            "20000.00"
                        ]
                    },
                    {
                        "name":"OS X 10.10",
                        "url":"",
                        "counts":[
                            "35000.00"
                        ]
                    },
                    {
                        "name":"Windows 8.1",
                        "url":"",
                        "counts":[
                            "50000.00"
                        ]
                    },
                    {
                        "name":"Windows 10",
                        "url":"",
                        "counts":[
                            "37000.20"
                        ]
                    },
                    {
                        "name":"OS X 10.9.5",
                        "url":"",
                        "counts":[
                            "19000.25"
                        ]
                    },
                    {
                        "name":"Windows Vista",
                        "url":"",
                        "counts":[
                            "20000"
                        ]
                    }

                        ]
                    }
                ]
            }
        ],
        "totals":[
            "27217371.38"
        ],
        "version":"1.4.15.11"
    },
    "waitSeconds":"1.390",
    "runSeconds":"5.021"
}

Solution

  • I assume you are asking how to calculate the percentage of the whole for each OS based on counts? The algorithm is simple, tally up the total counts from each object and then for any given object divide counts / totalcounts * 100.

    If you need help parsing the actual JSON data, here is a good question/answer for that:

    How to parse JSON in Java