0

I would like to create a Json structure manually using JsonObject and JsonArrays like the below:

{
 "data":[
         {
          "company_name":"xyz",
          "Amount":"$2000",
          "Duplicate_amount":"$500"
         },
         {
          "company_name":"abc",
          "Amount":"$5000"
         },
         {
          "company_name":"zzz",
          "Amount":"$2500",
          "Duplicate_amount":"$1000"
         }
      ]
}

The Json above is to be generated based on a checking done on an Arraylist. For example: Arraylist [xyz,abc,zzz,xyz,hhh,zzz]. Now I want to check, if the arraylist contains duplicate elements i.e here "xyz" and "zzz" then in the Json structure, the Duplicate_amount Json object to be added in the Json. Else if no duplicate present then only "company_name" and "amount" to be formed. The whole json format to be formed in this way.

How to do it? I have the logic for finding duplicate elements. But I cannot seem to find the logic for forming the above json based on the checking.

Thank you

Updates

So far I have tried this with checking. But the below code doesn't work and is not forming the appropriate json. What is the solution ?:

    JSONObject root_jsonObj = new JSONObject();
    JSONArray jsonArr = new JSONArray();
    JSONObject sub_jsonobj= new JSONObject();
    Object[] st = AppData.customer_arr.toArray();
    for (Object s : st) {

     //The if-else is the duplicate checking part here
        if (AppData.customer_arr.indexOf(s) != AppData.customer_arr.lastIndexOf(s)) {

            try {
                sub_jsonobj.put("name",AppData.customer_arr.get(counter));
                sub_jsonobj.put("dup_amount",AppData.amt_arr.get(counter));

            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        else{
            try {
                sub_jsonobj.put("name",AppData.customer_arr.get(counter));
                sub_jsonobj.put("amount",AppData.amt_arr.get(counter));

            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        jsonArr.put(sub_jsonobj);
        counter++;
    }
    try {
        root_jsonObj.put("data", jsonArr);

    } catch (Exception e) {
        e.printStackTrace();
    }
2
  • 1
    convert them into pojo and then compare them using equals method . Just include equals method in your POJO. Commented Oct 24, 2018 at 9:20
  • Can you give an example? It would be helpful Commented Oct 24, 2018 at 10:01

1 Answer 1

2

You can use this one.

JSONObject obj1 = new JSONObject();
try {
    obj1.put("company_name", "xyz");
    obj1.put("Amount", "$2000");
    obj1.put("Duplicate_amount", "$500");

} catch (JSONException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}

JSONObject obj2 = new JSONObject();
try {
    obj2.put("company_name", "xyz");
    obj2.put("Amount", "$2000");


} catch (JSONException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}


JSONArray jsonArray = new JSONArray();

jsonArray.put(obj1);
jsonArray.put(obj2);

JSONObject dataObj = new JSONObject();
    dataObj.put("Data", jsonArray);



String jsonStr = dataObj.toString();

    System.out.println("jsonString: "+jsonStr);
Sign up to request clarification or add additional context in comments.

2 Comments

I am sorry but it doesnot solve my question. I want to form the whole Json structure based on the duplicate checking if it contains duplicate or not in an arraylist. how to solve that?
you can parse the json to string than you can make your duplicate search.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.