简体   繁体   中英

POST JSON array with indices using retrofit

I'm using retrofit to POST a json array to server. Data is something like this:

{something:
    {somethings:
        [
            {"param1":"value", "param2":value},
            {"param1":"value", "param2":value}
        ]
    }
}

however, my server forces me to MUST include the indices in the array like:

{something:
    {somethings:
        {
         "0":{"param1":"value", "param2":value},
         "1":{"param1":"value", "param2":value}
        }
    }
}

In other words cannot send parameters like this:

something[somethings][][param1]
something[somethings][][param2]

I must include indices:

something[somethings][0][param1]
something[somethings][0][param2]

How to do this using retrofit?

My interface looks like this:

interface ApiService {
    @POST("endpoint")
    public Callback<Something> postSomething (@Body Something something);
}

My classes look like following:

public class PostSomething {
    private MapOfSomething something = new MapOfSomething();

    public MapOfSomething getSomething() {
        return portfolio;
    }

    public void setSomething(MapOfSomething something) {
        this.something = something;
    }
}

public class MapOfSomething {
    private JSONObject somethings = new JSONObject();

    public JSONObject getPortfolios() {
        return somethings;
    }

    public void setSomethings(List<Something> somethingList) {

        for (int i = 0; i<somethingList.size(); i++) {

            try {
                somethings.put(String.valueOf(i).toString(), somethingList.get(i));
            } catch (JSONException e) {
            e.printStackTrace();
            }
        }
    }

}

and calling the method like:

PostSomethings something = new PostSomethings();
MapOfSomething map = new mapOfSomething();
map.setSomethings(listofSomething);
something.setSomethings(map);
apiService.postSomething(something);

One way to solve your problem is to put json directly into body , so your interface will look like this

interface ApiService {
    @POST("endpoint")
    public Callback<Something> postSomething (@Body JSONObject jsonObject);
}

Now you need to create the JSONObject , here is how I have created your desired JSONObject

    JSONObject mainJson = new JSONObject();

    JSONArray list = new JSONArray();

    JSONObject singleElement1 = new JSONObject();

    try {
        singleElement1.put("param1","value1");
        singleElement1.put("param2","value2");
    } catch (JSONException e) {
        e.printStackTrace();
    }

    JSONObject singleElementSet1 = new JSONObject();

    try {
        singleElementSet1.put("1",singleElement1);
    } catch (JSONException e) {
        e.printStackTrace();
    }


    JSONObject singleElement2 = new JSONObject();

    try {
        singleElement2.put("param1","value1");
        singleElement2.put("param2","value2");
    } catch (JSONException e) {
        e.printStackTrace();
    }

    JSONObject singleElementSet2 = new JSONObject();

    try {
        singleElementSet2.put("2",singleElement2);
    } catch (JSONException e) {
        e.printStackTrace();
    }


    list.put(singleElementSet1);
    list.put(singleElementSet2);


    JSONObject subJson = new JSONObject();

    try {
        subJson.put("something",list);
        mainJson.put("something",subJson);
    } catch (JSONException e) {
        e.printStackTrace();
    }


    Log.d("json",""+mainJson.toString());

I am assuming right now you are calling your service like this way

  Call<Something> call = instanceOfYourAPIService.postSomething(anInstanceOfSomethingObject);

But now you have to replace this with the following

  Call<Something> call = instanceOfYourAPIService.postSomething(mainJson); //mainJson is the JSONObject which is created earlier

Hope it helps

EDIT

        JSONObject mainJson = new JSONObject();

        JSONObject singleElement1 = new JSONObject();

        try {
            singleElement1.put("param1","value1");
            singleElement1.put("param2","value2");
        } catch (JSONException e) {
            e.printStackTrace();
        }


        JSONObject singleElement2 = new JSONObject();

        try {
            singleElement2.put("param1","value1");
            singleElement2.put("param2","value2");
        } catch (JSONException e) {
            e.printStackTrace();
        }


        ArrayList<JSONObject> jsonObjectArrayList = new ArrayList<JSONObject>();

        //hope you can add item to this arraylist via some loop
        jsonObjectArrayList.add(singleElement1);
        jsonObjectArrayList.add(singleElement2);



        JSONArray list = new JSONArray();


        for(int i = 0;i<jsonObjectArrayList.size();i++){


            JSONObject elementSet = new JSONObject();
            try {
                elementSet.put(String.valueOf(i).toString(),jsonObjectArrayList.get(i));
            } catch (JSONException e) {
                e.printStackTrace();
            }

            list.put(elementSet);
        }


        JSONObject subJson = new JSONObject();

        try {
            subJson.put("something",list);
            mainJson.put("something",subJson);
        } catch (JSONException e) {
            e.printStackTrace();
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM