简体   繁体   中英

How to create the following JSON Structure dynamically

I am trying to create the following JSON Structure dynamically

 {
    "items": 
    {


        "Popcorn":
            [
                {
                    "name": "RegularPack"
                },
                {
                    "name": "Bucket"
                }

            ],
        "Chips":
            [
                {
                    "name": "HotChips"
                },
                {
                    "name": "UncleChips"
                }

            ]
    }

}

I have tried , but not sure of how to do that .

Could anybody please help me ??

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import org.json.JSONException;

public class TEST {

    public static void main(String[] args) throws JSONException {

        Map<String, ArrayList<String>> data = new HashMap<String, ArrayList<String>>();

        ArrayList<String> popcornlist = new ArrayList<String>();
        popcornlist.add("Regular Pack");
        popcornlist.add("Bucket");

        ArrayList<String> chipslist = new ArrayList<String>();
        chipslist.add("HotChips");
        chipslist.add("Uncle Chips");

        data.put("Popcorn", popcornlist);
        data.put("Chips", chipslist);


        Iterator iterator=data.entrySet().iterator();

        while(iterator.hasNext()){
          Map.Entry e = ( Map.Entry ) iterator.next ( ) ;
          System.out.println("key: "+e.getKey());
        }
    }
}

Could anybody please tell me where to Open the JSONObject and JSONArray ??

I am using org.json for this .

Please see the following link which indicates when to use JSONObject and JSONArray . It may be easier to send a java object back to the front end and use some client side code to parse it to a JSON object using GSON

Encoding JSON in Java

import org.json.simple.JSONObject;

class JsonEncodeDemo 
{
   public static void main(String[] args) 
   {
      JSONObject obj = new JSONObject();

      obj.put("name", "foo");
      obj.put("num", new Integer(100));
      obj.put("balance", new Double(1000.21));
      obj.put("is_vip", new Boolean(true));

      System.out.print(obj);
   }
}

Output

{"balance": 1000.21, "num":100, "is_vip":true, "name":"foo"}

Decoding JSON in Java

import org.json.simple.JSONObject;
import org.json.simple.JSONArray;
import org.json.simple.parser.ParseException;
import org.json.simple.parser.JSONParser;

class JsonDecodeDemo 
{
   public static void main(String[] args) 
   {
      JSONParser parser=new JSONParser();
      String s = "[0,{\"1\":{\"2\":{\"3\":{\"4\":[5,{\"6\":7}]}}}}]";
      try{
         Object obj = parser.parse(s);
         JSONArray array = (JSONArray)obj;
         System.out.println("The 2nd element of array");
         System.out.println(array.get(1));
         System.out.println();

         JSONObject obj2 = (JSONObject)array.get(1);
         System.out.println("Field \"1\"");
         System.out.println(obj2.get("1"));    

         s = "{}";
         obj = parser.parse(s);
         System.out.println(obj);

         s= "[5,]";
         obj = parser.parse(s);
         System.out.println(obj);

         s= "[5,,2]";
         obj = parser.parse(s);
         System.out.println(obj);
      }catch(ParseException pe){
         System.out.println("position: " + pe.getPosition());
         System.out.println(pe);
      }
   }
}

Output

The 2nd element of array
{"1":{"2":{"3":{"4":[5,{"6":7}]}}}}

Field "1"
{"2":{"3":{"4":[5,{"6":7}]}}}
{}
[5]
[5,2]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM