如何以POST形式提交JSON数据

2024-12-03 00:29:30
推荐回答(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;
}

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

retrieveJSONArray(ArrayList<> jsonArray,String[] key)


*
*
  • 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: [{"key[0]","1st element of array"},{"key[1]","2nd element of array"}]

  • *

*/
//========================================================================
public static String retrieveJSONArray(ArrayList jsonArray,String[] key){

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

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

retrieveJSONString(ArrayList<> jsonArray)


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

  • *
  • jsonArray will be ArrayList of array.

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

  • *

*/
//========================================================================
public static String retrieveJSONString(ArrayList jsonObject){

try{

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

stringer.object();

for(int i=0;i
arr_jsonObject=jsonObject.get(i);
stringer.key(arr_jsonObject[0]).value(arr_jsonObject[1]);
}

stringer.endObject();

return stringer.toString();

}catch(Exception e){

e.printStackTrace();
}
return null;
}

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

Converts jsonArray to an arrayList of String[]. Where each row contains values in json
* String array, in increasing order of key values provided, without there key counterparts.
*
* For ex: if JSON string is [{"key00":"value00","key01":"value01"},{"key10":"value10","key11":"value11"}],
* then the rows of an array will be as follows
*

  • First row : 1st element- value00, 2nd element - value01

  • *
  • Second row : 1st element- value10, 2nd element - value11

*


*
* */
//========================================================================
public static ArrayList convertJSONArraytoArrayList(String jsonArray,String[] key){

try{

JSONArray JsonArray=new JSONArray(jsonArray);
JSONObject JsonObject=new JSONObject();

int jsonArraySize=JsonArray.length();

String[] jsonObjectArray;
ArrayList jsonArrayList=new ArrayList();

for(int i=0;i
JsonObject=JsonArray.getJSONObject(i);

jsonObjectArray=new String[key.length];

for(int j=0;j jsonObjectArray[j]=JsonObject.getString(key[j]);

jsonArrayList.add(jsonObjectArray);
}

return jsonArrayList;

}catch(Exception e){

e.printStackTrace();
return null;
}
}

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

Converts jsonString to an arrayList of String[].
*
* For ex: if JSON string is {"key00":"value00","key01":"value01"},
* then the rows of an array will be as follows
*

  • First row : 1st element- value00

  • *
  • Second row : 1st element- value10

*


*
* */
//========================================================================
public static ArrayList convertJSONStringtoArrayList(String jsonString,String[] key){

try{

JSONObject jsonObject=new JSONObject(jsonString);

ArrayList jsonArrayList=new ArrayList();

for(int i=0;i jsonArrayList.add(new String[]{jsonObject.getString(key[i])});

return jsonArrayList;

}catch(Exception e){

e.printStackTrace();
return null;
}
}

}
如果我的回答没能帮助您,请继续追问。
转载,仅供参考。