0

I have exising json data which contains json array:

 {
    "dataSetId":3,
    "Viewable":null,
    "Charge":null,
    "tax":null,
    "Cards":[ //need to remove or update json array Cards here
    {
    "Id":605,
    "startCount":1,
    "endCount":500,
    "Party":0.25,
    "non-Party":0.375,
    "X-Party":1.25,
    "Y-Party":1.625
    },
    {
    "Id":605,
    "startCount":1,
    "endCount":500,
    "Party":0.28,
    "non-Party":0.377,
    "X-Party":1.26,
    "Y-Party":1.626
    }]
    }

Now, i want to insert the new json array created at Json Element Cards above. Following i tried :

JsonObject orignalJson = gson.fromJson(existingJson.asString(), JsonElement.class).getAsJsonObject();
        System.out.println("orignalJson :: "+orignalJson);

    JsonObject dataElementJson = orignalJson.get("data").getAsJsonObject();
    JsonArray RateJsonArray = dataElementJson.get("Cards").getAsJsonArray();

How the index is identified in orignaljson and update Cards (Json Array)?

ButterSkotch
  • 75
  • 1
  • 1
  • 6
  • The code you've provided only extracts data from JSON. Did you try https://static.javadoc.io/com.google.code.gson/gson/2.6.2/com/google/gson/JsonObject.html#remove-java.lang.String- and https://static.javadoc.io/com.google.code.gson/gson/2.6.2/com/google/gson/JsonObject.html#add-java.lang.String-com.google.gson.JsonElement- ? – Lyubomyr Shaydariv Mar 24 '17 at 12:00
  • I have tried both of them it removes the element at that index but after removing i dont know the index where i removed the value. So, as mentioned above, i need to add newly created json at that index. I think if i add anonymously, then it will add at the end of json object. – ButterSkotch Mar 24 '17 at 12:11
  • You mean numerical index in the cards array or the top-most object property names? – Lyubomyr Shaydariv Mar 24 '17 at 12:14
  • I need index of Cards in this Complete Json and update the newly created json array. Do i need to deserialize the orignal json? I also removed and added property using addProperty, this adds at the end of json . – ButterSkotch Mar 24 '17 at 12:19
  • Sorry, can you elaborate what you really mean by _index_? Just the position of the `Cards` property in the top-most object? – Lyubomyr Shaydariv Mar 24 '17 at 12:29
  • I got this resolved. I was thinking it would check the order before inserting the array.but its not. – ButterSkotch Mar 24 '17 at 13:02
  • No, JSON objects are not really intended to be ordered data bags. However, the order _may_ matter if its read as a stream. See more: http://stackoverflow.com/questions/16870416/does-the-sequence-of-the-values-matter-in-a-json-object – Lyubomyr Shaydariv Mar 24 '17 at 13:15

0 Answers0