0

With the values present inside the Map DataStructure i am creating a JSON dynamically .

The JSON its getting created is

{
    "item": {
        "T1": [
            {
                "name": "Ice creams",
                "T2": [
                    {
                        "name": "Ice creams***Stick",
                        "T3": [
                            {
                                "T4": [
                                    {
                                        "name": "Ice creams***Stick***KoolCool***Strawbeerry",
                                        "leaf": [
                                            {
                                                "crust_name": "crust"
                                            }
                                        ]
                                    }
                                ],
                                "name": "Ice creams***Stick***KoolCool"
                            }
                        ]
                    }
                ]
            }
        ]
    }
}

The problem is that the name under T3 is being appended at some other place rather than after it ,which should actually be

{
    "item": {
        "T1": [
            {
                "name": "Ice creams",
                "T2": [
                    {
                        "name": "Ice creams***Stick",
                        "T3": [
                            {
                                "name": "Ice creams***Stick***KoolCool",
                                "T4": [
                                    {
                                        "name": "Ice creams***Stick***KoolCool***Strawbeerry",
                                        "leaf": [
                                            {
                                                "crust_name": "crust"
                                            }
                                        ]
                                    }
                                ]
                            }
                        ]
                    }
                ]
            }
        ]
    }
}

Functionally there should be no difference between name being first or second in the property list,but this JSON would be passed to the Front End and the search functionality is breaking down ,if it not follows the structure

could anybody please let me know how to make the name appear after T3 ??

Please see this fiddle

http://jsfiddle.net/5wvqkb82/

This is my Java Program.

package com.services;

import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

public class Test {

    private static JSONObject processString(String data, int level,String key) throws JSONException {
        JSONObject json = new JSONObject();
        JSONArray leafjsonarray = new JSONArray();
        int index = data.indexOf(',');
        String name = data;
        String remainder = "";
        if (index < 0) {
            index = name.indexOf('(');
            if (index > 0) {
                name = data.substring(0, index);
            }
        } else {
            name = data.substring(0, index);
            remainder = data.substring(name.length() + 1);
        }
        String fullpath = key+"***"+name;
        json.put("name", fullpath);

        JSONArray a = new JSONArray();
        if (remainder.length() > 0) {
            a.put(processString(remainder, level + 1,fullpath));
            json.put("T" + level, a);
        }

        else {


             JSONObject leafjsonObj = new JSONObject();
             leafjsonObj.put("crust_name", "crust");
             leafjsonarray.put(leafjsonObj);
            json.put("leaf", leafjsonarray);


        }
        return json;
    }  

    private static JSONArray processList(List<String> list, int level,String key) throws JSONException {
        JSONArray json = new JSONArray();
        for (String data : list) {
            json.put(processString(data, level,key));
        }
        return json;
    }  

    private static JSONArray processMap(Map<String, List<String>> map, int level) throws JSONException {

        JSONArray array =new JSONArray(); 
         for (String key : map.keySet()) { 
              JSONObject json = new JSONObject(); 
              json.put("name", key);      
              json.put("T" + level, processList(map.get(key), level + 1,key)); 
              array.put(json); 
         } 
         return array;

    }        

    public static void main(String args[]) {
        Map<String, List<String>> consilatedMapMap = new LinkedHashMap<String, List<String>>();

        List<String> values = new LinkedList<String>();
        values.add("Stick,KoolCool,Strawbeerry(25)");
      //  values.add("Cone,SSS(25)");

       /* List<String> values2 = new LinkedList<String>();
        values2.add("Bucket(25)");
      */

        //consilatedMapMap.put("Popcorn", values2);
        consilatedMapMap.put("Ice creams", values);


        try {
            int level = 2;
            JSONArray json = processMap(consilatedMapMap, level);
            JSONObject jsonT1 = new JSONObject();
            jsonT1.put("T1",json);
            JSONObject sai = new JSONObject();
            sai.put("item",jsonT1);

            System.out.println(sai);

        } catch(JSONException x) {
            x.printStackTrace();
            System.exit(-1);
        }
}
}
DSM
  • 342,061
  • 65
  • 592
  • 494
Pawan
  • 31,545
  • 102
  • 256
  • 434

1 Answers1

1

You need to take a look at this. Basically, you cannot rely on the order of the objects. If you need them to be in an order, you should use JSON Arrays.

If you want to use Arrays, your whole ordering will be messed up, for example:

 "T4": [
      {
          "name": "Ice creams***Stick***KoolCool***Strawbeerry",
          "leaf": [
              {
                 "crust_name": "crust"
              }
           ]
       }
   ]

will become

 "T4": [
       { 
          "name": "Ice creams***Stick***KoolCool***Strawbeerry"
       },
       {
          "leaf": [
               {
                  "crust_name": "crust"
               }
           ]
        }
   ]

You have used JSON Arrays in your program, make use of it and figure it out if you really want to go ahead with this way.

Community
  • 1
  • 1
NV Bhargava
  • 373
  • 2
  • 10