How to Create Correct JSONarray in Java Using JSONobject

How to create correct JsonArray in Java using JSONObject using servlet

{
"title": "ZAKI",
"start": "2010-05-01",
"placemarks": [
{
"polyline": [
{
"lat": 48.2246726495652,
"lon": 16.32568359375
},
{
"lat": 47.5172006978394,
"lon": 18.984375
},
{
"lat": 45.844107795602,
"lon": 15.9521484375
},
{
"lat": 48.2246726495652,
"lon": 16.32568359375
}
]
},
{
"point": {
"lat": 48.2246726495652,
"lon": 16.32568359375
}
},
{
"point": {
"lat": 47.5172006978394,
"lon": 18.984375
}
},
{
"point": {
"lat": 45.844107795602,
"lon": 15.9521484375
}
}
]
}

First of All use some formatting tool to format your json so that you can easily understand the difference in expected vs actual output.

for the polyline object you should have an array rather than object with in a object.

Json.createObjectBuilder().add("polyline", 
Json.createObjectBuilder().add("lat", lat).add("lon", lon))
)

create JSONArray from JSONObject

In the problematic line, instead of casting to a JSONArray, use getJSONArray:

JSONArray JSONFirewallRules = jsonObject.getJSONArray(jsonStrings.REQUEST_RULES_ALL_RESPONSE); 

However the exception isn't a cast exception, but a constructor exception where you are trying to build a JSONArray object from an unsupported list of items, which is another JSONArray :)

How to generate JsonArray correctly?

Just replace avaliacoes.add(avaliacao); with avaliacoes.put(avaliacao);

public void enviaDadosVenda(){
JSONObject obj = new JSONObject();
JSONArray avaliacoes = new JSONArray();
JSONObject avaliacao;

try {
obj.put("id", 0);
obj.put("ticket", "DemoActivity.ticket_id");

for(int i=0; i < 2;i++){
avaliacao = new JSONObject();
avaliacao.put("idAvaliacao", "1");
avaliacao.put("nota", "nota");
avaliacao.put("observacao", "observacao");
avaliacoes.put(avaliacao);

}
obj.put("avaliacoes", avaliacoes);
Log.d("DEMO", obj.toString()); // {"id":0,"ticket":"DemoActivity.ticket_id","avaliacoes":[{"idAvaliacao":"1","nota":"nota","observacao":"observacao"},{"idAvaliacao":"1","nota":"nota","observacao":"observacao"}]}

} catch (JSONException e) {
e.printStackTrace();
}
}

For best practice use Gson

Create a JSONArray

But how can I create a JSONArray and insert it to the JSONObject?

You can create JSONArray same like you have tried to create JSONObject.

Creating time:

For example:

JSONArray myArray = new JSONArray();
JSONObject j = new JSONObject();
j.put("key",value);
j.put("array",myArray);

Retrieving time:

you can fetch the value of String or JSONObject or any by their key name. For example:

JSONArray myArray = objJson.getJSONArray("array");

add JSONArray within a JSONObject

I believe the problem is that you're using org.json.alt.JSONArray instead of org.json.JSONArray. I'm not familiar with that class, but I suspect JSONObject.put is just calling toString() on it rather than treating it as an existing JSON array. Here's a short but complete example that doesn't have the problem:

import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONArray; // Note the import here

public class Test {
public static void main(String[] args) throws JSONException {
JSONArray playerIds = new JSONArray();
playerIds.put("a");
playerIds.put("b");
JSONObject notification = new JSONObject();
notification.put("include_player_ids", playerIds);
System.out.println(notification);
}
}

Output:

{"include_player_ids":["a","b"]}

How to create JSON Object using String?

org.json.JSONArray may be what you want.

String message;
JSONObject json = new JSONObject();
json.put("name", "student");

JSONArray array = new JSONArray();
JSONObject item = new JSONObject();
item.put("information", "test");
item.put("id", 3);
item.put("name", "course1");
array.put(item);

json.put("course", array);

message = json.toString();

// message
// {"course":[{"id":3,"information":"test","name":"course1"}],"name":"student"}


Related Topics



Leave a reply



Submit