I want to create a JSONObject like this:
[
{
id:01,
name:"John",
number:010
},
{
id:02,
name:"Mike",
number: 020
}
]
This is my code:
public void equipmentViewed(List<Equipment> equipmentSelected, final OnControlResponseListener listener, String description, Equipment equipment) throws JSONException {
wsAccessControl = WSAccessControl.getInstance();
EquipmentViewed equipmentViewed = new EquipmentViewed();
equipmentViewed.setEquipment(equipmentsCount(equipmentSelected));
JSONObject jsonObject = new JSONObject();
try {
jsonObject.put("", new JSONArray(equipmentViewed.getEquipment().toString()));
} catch (JSONException e) {
Log.e(TAG, "Failed to create json object. Cause: " + e.getMessage());
}
String url = Constants.PROVIDER_DOMAIN_URL + Constants.REQUEST_EQUIPMENT;
wsAccessControl.makeWSRequest(RequestType.POST, url, jsonObject, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
listener.OnResponseReceived(response);
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
listener.OnResponseError(error);
}
}, true);
}
Where EquipmentViewed contains a String list equipment.
Mohammed Aouf Zouag
17.2k4 gold badges44 silver badges68 bronze badges
asked Nov 19, 2015 at 15:13
Eduardo Bonfa
2881 gold badge4 silver badges15 bronze badges
3 Answers 3
You can create the JSON you want using this:
JSONArray array = new JSONArray();
JSONObject obj1 = new JSONObject();
obj1.put("id", "01");
obj1.put("name", "John");
obj1.put("number", "010");
JSONObject obj2 = new JSONObject();
obj2.put("id", "02");
obj2.put("name", "Mike");
obj2.put("number", "020");
array.put(obj1);
array.put(obj2);
/* array = [
{
id:01,
name:"John",
number:010
},
{
id:02,
name:"Mike",
number: 020
}
]
*/
answered Nov 19, 2015 at 15:23
Mohammed Aouf Zouag
17.2k4 gold badges44 silver badges68 bronze badges
Sign up to request clarification or add additional context in comments.
1 Comment
Eduardo Bonfa
I used Gson, way easier. But Thank you anyway
You can use the JSONTokener for this Task.
try{
String json = equipmentViewed.getEquipment().toString();
JSONArray object = (JSONArray) new JSONTokener(json).nextValue();
JSONObject firstEntry = (JSONObject) object.get(0);
JSONObject sndEntry = (JSONObject) object.get(1);
}catch (JSONException ex){
//TODO handle Error here
}
If your equipmentViewed.getEquipment().toString()returns the following:
/* String json =
[
{
id:01,
name:"John",
number:010
},
{
id:02,
name:"Mike",
number: 020
}
] */
3 Comments
Eduardo Bonfa
but in this case something like this is created: { id: [ { 010; 020 } ] right?
woley
sry I got your question wrong and edited the answer. Does this solve your problem?
Eduardo Bonfa
I used Gson, way easier. But Thank you anyway
You can try something like this :
JsonArray jsonArray = new JsonArray();
for (int i = 0; i < equipmentViewed.getEquipment().size(); i++) {
JsonObject jsonObject = new JsonObject();
jsonObject.put("id", equipmentViewed.getEquipment().get(i).getId());
jsonObject.put("name", equipmentViewed.getEquipment().get(i).getName());
jsonObject.put("number", equipmentViewed.getEquipment().get(i).getNumber());
jsonArray.put(jsonObject);
}
You will have all the data in your JsonArray object jsonArray.
I am assuming you the objects in your equipmentViewed.getEquipment() list have those getter methods.
All the best :)
answered Nov 19, 2015 at 17:33
Narayan Acharya
1,4991 gold badge18 silver badges34 bronze badges
1 Comment
Eduardo Bonfa
I used Gson, way easier. But Thank you anyway
lang-java
equipmentViewed.getEquipment()? please share