Put JSONObjet into JSONArray

how to add multiple json object in json array in java
add json object to json array c#
how to add json object to arraylist in java
how to store json object in arraylist in java
add jsonarray to jsonobject gson
add json object to json array python
how to add json array to another json array
add json object to json array php

I have a problem putting JSONObject into JSONArray.

Here is my code:

String strObject = "{\"Code\":\"a\", \"Name\": \"b\"}";
JSONObject anExistedObject = new JSONObject(strObject);
JSONArray myArray = new JSONArray();

for(int count = 0; count < 2; count++){
    JSONObject myObject = new JSONObject;
    myObject = anExistedObject;

    myObject.put("Count", count);
    myArray.put(myObject);
}

System.out.println(myArray.toString());

Result:

[
    {
        "Code": "a",
        "Name": "b",
        "Count": 1
    },
    {
        "Code": "a",
        "Name": "b",
        "Count": 1
    }
]

What I expected:

[
    {
        "Code": "a",
        "Name": "b",
        "Count": 0
    },
    {
        "Code": "a",
        "Name": "b",
        "Count": 1
    }
]

I've read this post but still don't know how to fix mine. Did I missed something?


You are updating and reusing anExistedObject everytime

String strObject = "{\"Code\":\"a\", \"Name\": \"b\"}";
JSONArray myArray = new JSONArray();

for(int count = 0; count < 2; count++){
    JSONObject myObject = new JSONObject(strObject);

    myObject.put("Count", count);
    myArray.put(myObject);
}

System.out.println(myArray.toString());

How to put/get multiple JSONObjects to JSONArray?, JSONArray.toString. Common ways to obtain JSONArray Put a value in the JSONArray, where the value will be a JSONObject which * is produced from a Map. JavaJSONObject Oriented ProgrammingProgramming. The JSON is a text-based format for exchanging data. It is a lightweight component and language independent. We can also add a JSONArray to JSONObject. We need to add a few items to an ArrayList first and pass this list to the put () method of JSONArray class and finally add this array to JSONObject using the put () method.


You each time modify same object because of line: myObject = anExistedObj; You need make copy of that object instead.

Correct code:

JSONObject anExistedObj = new JSONObject();
myObject.put("Code", "a");
myObject.put("Name", "a");

JSONArray myArray = new JSONArray();
String[] keys = JSONObject.getNames(anExistedObj);

for(int count = 0; count < 2; count++){
    JSONObject myObject = new JSONObject(anExistedObj, keys);
    //anExistedObj = {"Code":"a", "Name": "b"}

    myObject.put("Count", count);
    myArray.put(myObject);
}

System.out.println(myArray.toString());

Checkout documentation of copy constructor JSONObject

org.json.JSONArray.put java code examples, We need to add a few items to an ArrayList first and pass this list to the put() method of JSONArray class and finally add this array to JSONObject  I'm retrieving some JSON data from my server. Sometimes this can come in the form of a JSONObject (one objects) or a JSONArray (multiple objects). I'm aware of how to detect if it is an array or an object but the problem is that detecting it means that I have to have two completely separate bits of logic, one for objects and one for arrays.


This happens because the line myObject.put("Count", count); always modifies the same object since the variable is only a reference to the object itself. Meaning that myObject and anExistedObject point to the same object.

You should create a copy with:

JSONObject copy = new JSONObject(original, JSONObject.getNames(original));

Instead of using:

JSONObject myObject = new JSONObject;
myObject = anExistedObj;

How can we add a JSONArray to JSONObject in Java?, assertOnUiThread(); JSONObject result = new JSONObject(); result.put("n", view. Merge the event info with the instances and turn it into a JSONArray. I am using a flow to check the changes within a sharepoint list by looking at the version history of the field changing. I seem to get the HTTP request correct but when trying to use the output it is in the form of an object, not an array. Any ideas on generating the output of the HTTP request as an


Java Code Examples org.json.JSONArray.put, Android Add JSONArray to JSONObject. Example#. // Create a new instance of a JSONArray JSONArray array = new JSONArray(); // With put() you can add a  Insert the required key-value pairs using the put() method of the JSONObject class. jsonObject.put("key", "value"); Create a JSON array by instantiating the JSONArray class and add, elements to the created array using the add() method of the JSONArray class.


Android, From return Table From SP then. Hide Copy Code. JSONArray obj = new JSONArray(); try { for(int i = 0; i <table.rows.count; i++) { // 1st object  Put(Double) Put(Double) Appends value to the end of this array.. Put(Int32, Int64) Put(Int32, Int64) Sets the value at index to value, null padding this array to the required length if necessary.


I want to add a new JSON object to the already existing JSON Array , JSONArray , JSONObject , Number , String , or the JSONObject.NULL object. A JSONObject constructor can be used to convert an external form JSON text into  The following are Jave code examples for showing how to use put() of the org.json.JSONArray class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.