如何通过retrofit提交json格式数据

2025-04-05 11:37:49
推荐回答(1个)
回答1:

import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONObject;
import org.json.JSONStringer;

import android.util.Log;

public class JSON{

//========================================================================
/**
*

retrieveJSONArray(ArrayList<> jsonArray)


*
*
  • Returns JSON formed Array from the ArrayList provided.

  • *
  • jsonArray will be ArrayList of array.

  • *
  • the elements provided in array will be arranged in consecutive keys

  • *
  • ex: [{"key0","1st element of array"},{"key1","2nd element of array"}]

  • *

*/
//========================================================================
public static String retrieveJSONArray(ArrayList jsonArray){

try{

String[] jsonObject=new String[2];
JSONStringer stringer=new JSONStringer();

stringer.array();

int arrayLength=jsonArray.size();

for(int i=0;i
jsonObject=jsonArray.get(i);

stringer.object();

for(int j=0;j stringer.key("key"+j).value(jsonObject[j]);

stringer.endObject();
}

stringer.endArray();

return stringer.toString();

}catch(Exception e){

e.printStackTrace();
}
return null;
}