使用retrofit的索引POST JSON数组

时间:2016-10-22 09:46:00

标签: android retrofit

我正在使用改造将json数组POST到服务器。数据是这样的:

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

但是,我的服务器强迫我必须在数组中包含索引,如:

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

换句话说,不能发送这样的参数:

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

我必须包含指数:

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

如何使用改造来做到这一点?

我的界面如下所示:

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

我的课程如下:

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();
            }
        }
    }

}

并调用方法如:

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

1 个答案:

答案 0 :(得分:1)

解决问题的一种方法是将json直接放入body中,因此您的界面将如下所示

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

现在你需要创建JSONObject,这就是我创建你想要的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());

我现在假设您正在以这种方式呼叫您的服务

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

但现在你必须用以下

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

希望有所帮助

修改

        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();
        }