2016-10-22 61 views
0

我正在使用retrofit POST json数组到服务器。数据是这样的:POST JSON数组索引使用改进

{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); 
+0

你的第二个JSON格式是错误的。里面的json数组,你不能把像0这样的值:{“param1”:“value”,“param2”:value} this。 –

+0

是的语法可能是错的,我不是很熟悉它。但关键是,服务器期望数组的索引。我不能POST数组没有索引。 –

+0

然后您可以手动创建json并以字符串形式发送 –

回答

1

的一种方式解决你的问题EM是直接把JSON进入体内,使你的界面看起来像这样

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

感谢您的回答。但它看起来像你认为我的JSON数组中只有两个元素。实际上它可以是数组中的一千个元素 –

+0

我不完全知道数组中元素的数量 –

+0

我假设您通过迭代列表或数组列表来创建此类数组。检查我的编辑,通过它你可以有任何数量的元素 –