4

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
2
  • What String is returned from equipmentViewed.getEquipment() ? please share Commented Nov 19, 2015 at 15:21
  • it's a list of information, like, name, model, serialNumber, location Commented Nov 19, 2015 at 15:23

3 Answers 3

5

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
Sign up to request clarification or add additional context in comments.

1 Comment

I used Gson, way easier. But Thank you anyway
2

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
 }
 ] */
answered Nov 19, 2015 at 15:26

3 Comments

but in this case something like this is created: { id: [ { 010; 020 } ] right?
sry I got your question wrong and edited the answer. Does this solve your problem?
I used Gson, way easier. But Thank you anyway
1

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

1 Comment

I used Gson, way easier. But Thank you anyway

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.