0
            results = {ArrayList@22831}  size = 11
             0 = {LinkedTreeMap@22840}  size = 36
              0 = {LinkedTreeMap$Node@22854} "id" -> "7.0"
              1 = {LinkedTreeMap$Node@22855} "version" -> "0.0"
              2 = {LinkedTreeMap$Node@22856} "family" -> " size = 9",
        .
        .
        .

              35 = "formattedValue"-> "[  
       {  
          "subCondition":"default",
          "maxGuarantee":{  
             "amount":2500000,
             "currencyCode":"USD"
          },
          "minGuarantee":{  
             "amount":30000,
             "currencyCode":"USD"
          },
          "minLoanAmount":{  
.
.
.

I need to add

 "minGuaranteePerPerson":{  
             "amount":30000,
             "currencyCode":"USD"
          },

to add the bottom of that array i gave at the first. So, it will be in formattedValue. At the end, it will be like this:

35 = "formattedValue"-> "[  
           {  
              "subCondition":"default",
              "maxGuarantee":{  
                 "amount":2500000,
                 "currencyCode":"USD"
              },
              "minGuarantee":{  
                 "amount":30000,
                 "currencyCode":"USD"
              },
              "minLoanAmount":{  
...
     },"minGuaranteePerPerson":{  
                 "amount":30000,
                 "currencyCode":"USD"
              },

I tried this:

 final JsonObject json = (JsonObject) results.get(i).get("formattedValue").toString()

but it gave error of like Can't cast gson to my Object type

Probably because it is not JSONOBject, it is array.

results 

is like this

ArrayList<LinkedTreeMap<String, Object>> results;

I need to first get that JSon, that to add my variable but i can not get.

Can not use objectmapper because there is no DTO or class. I need to do with add operations.

When i do

new JSONArray(results)

it shows:

[{"code":"KOBI","componentBases":[],"formattedValue":"[{\"subCondition\":\"default\",\"minGuaranteeRate\":\"15\",\"maxGuaranteeRate\":\"25\",\"maxGuaranteeAmount\":{\"amount\":2500000,\"currencyCode\":\"TRY\"},//and so on

when i do

new JSONArray(results).getJSONObject(0)

it shows only first one.

new JSONObject(results)

this shows empty false

1 Answer 1

1

It is difficult to help you if you don't tell us what JSON library you are using nor provide "clean" JSON format input. Also, it looks like you don't want to add your data to an array but to an object contained in an array.

Basically, what you have to do is to navigate to the element you want to modify, e.g.

JsonObject formattedValueFirst = results.get(0).get("formattedValue").get(0);

and then modify it, e.g.

JsonObject minGuaranteePerPersonObject = new JsonObject();
minGuaranteePerPersonObject.addProperty("amount", 30000);
minGuaranteePerPersonObject.addProperty("currencyCode", "USD");
formattedValueFirst.addProperty("minGuaranteePerPerson", minGuaranteePerPersonObject);
Sign up to request clarification or add additional context in comments.

2 Comments

When i do this new JsonObject(results.get(0).get("formattedValue")), it says invalid argument and last get(0) is not coming. Jackson library is jackson-databind-2.9.6.jar
After re-reading your question I see that you are not dealing with JSON* Objects but have ArrayLists and so on. This means that .get("formattedValue") will return the type Object. You have to find out what the actual (run-time) type of this Object is and cast it to something more usable first.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.