Search code examples
javaarraysjsonparsingjson-simple

Java Parse Json Array in another Json Array json.simple


I'm using JSON.simple lib and try to parse a JSON response from HTTP GET. It works fine but I struggle decoding the following structure

"example": [
    {
      "param1": 4.88,
      "param2": 60,
      "param3": [
        {
          "param3_1": 501,
          "param3_2": "Rain",
        }
      ],
    },

I manage to extract param1 and param2 successfully with following code:

            JSONObject jo = (JSONObject) new JSONParser().parse(jsonString);
            JSONArray ja;
            Iterator<Map.Entry> itMap;
            Iterator itArray;

            ja = (JSONArray) jo.get("example");
            if (ja != null) {
                itArray = ja.iterator();
                while (itArray.hasNext()) {
                    ExampleClass e = new ExampleClass();
                    itMap = ((Map) itArray.next()).entrySet().iterator();
                    while (itMap.hasNext()) {
                        Map.Entry pair = itMap.next();
                        switch (pair.getKey().toString()) {
                            case "param1":
                                e.param1 = (double)pair.getValue();
                                break;
                            case "param2":
                                e.param2 = (long)pair.getValue();
                                break;
                            case "param3":
                                .....
                                break;
                            default:
                                break;
           }}}}

Does anyone know how to set up the iterator to get the param3 values?


Solution

  • This is far more easier if you use a library like GSON. However, if you wish to continue as it is, following code would extract the content within param3. This is not a recommended way as the code would have to change if the attributes within the node gets changed.

    Therefore, please try to use a JSON Parser next time

                            case "param3":
                            JSONArray jsonArray = ( JSONArray ) pair.getValue();
                            for( Object object : jsonArray )
                            {
                                JSONObject jsonObject = ( JSONObject ) object;
                                jsonObject.keySet().forEach( o ->
                                {
                                    if( "param3_1".equalsIgnoreCase( o.toString() ) )
                                    {
                                        // extract your value here
                                        long next = ( long ) jsonObject.get( o );
                                        System.out.println( next );
                                    }
                                    else if( "param3_2".equalsIgnoreCase( o.toString() ) )
                                    {
                                        // extract your value here
                                        String next = String.valueOf( jsonObject.get( o ) );
                                        System.out.println( next );
                                    }
                                } );
                            }
                            break;
    

    Follow up this link if you wish to add GSON to this. This is a pretty easy and straight forward tutorial