如何以POST形式提交JSON数据

 我来答
匿名用户
2015-06-26
展开全部
很高兴为您解答,方法好下:
import java.util.ArrayList;

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

import android.util.Log;

public class JSON{

//========================================================================
/**
* <b><p> retrieveJSONArray(ArrayList<</String[]>> jsonArray)</b></p>
*
* <ul><li>Returns JSON formed Array from the ArrayList provided.</li>
* <li><b>jsonArray</b> will be ArrayList of array.</li>
* <li>the elements provided in array will be arranged in consecutive keys</li>
* <li>ex:<b> [{"key0","1st element of array"},{"key1","2nd element of array"}]</b> </li>
* </ul>
*/
//========================================================================
public static String retrieveJSONArray(ArrayList<String[]> jsonArray){

try{

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

stringer.array();

int arrayLength=jsonArray.size();

for(int i=0;i<arrayLength;i++){

jsonObject=jsonArray.get(i);

stringer.object();

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

stringer.endObject();
}

stringer.endArray();

return stringer.toString();

}catch(Exception e){

e.printStackTrace();
}
return null;
}

//========================================================================
/**
* <b><p> retrieveJSONArray(ArrayList<</String[]>> jsonArray,String[] key)</b></p>
*
* <ul><li>Returns JSON formed Array from the ArrayList provided.</li>
* <li><b>jsonArray</b> will be ArrayList of array.</li>
* <li>the elements provided in array will be arranged in consecutive keys</li>
* <li>ex:<b> [{"key[0]","1st element of array"},{"key[1]","2nd element of array"}]</b> </li>
* </ul>
*/
//========================================================================
public static String retrieveJSONArray(ArrayList<String[]> jsonArray,String[] key){

try{

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

stringer.array();

int arrayLength=jsonArray.size();

for(int i=0;i<arrayLength;i++){

jsonObject=jsonArray.get(i);

stringer.object();

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

stringer.endObject();
}

stringer.endArray();

return stringer.toString();

}catch(Exception e){

e.printStackTrace();
}
return null;
}

//========================================================================
/**
* <b><p> retrieveJSONString(ArrayList<</String[]>> jsonArray)</b></p>
*
* <ul><li>Returns JSON formed string from the ArrayList provided.</li>
* <li><b>jsonArray</b> will be ArrayList of array.</li>
* <li>ex:<b> {"key0":"1st element of array","key1":"2nd element of array"}</b> </li>
* </ul>
*/
//========================================================================
public static String retrieveJSONString(ArrayList<String[]> jsonObject){

try{

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

stringer.object();

for(int i=0;i<jsonObject.size();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;
}

//========================================================================
/**
* <p>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
* <ul><li>First row : 1st element- value00, 2nd element - value01</li>
* <li>Second row : 1st element- value10, 2nd element - value11</li></ul>
* </p>
*
* */
//========================================================================
public static ArrayList<String[]> convertJSONArraytoArrayList(String jsonArray,String[] key){

try{

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

int jsonArraySize=JsonArray.length();

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

for(int i=0;i<jsonArraySize;i++){

JsonObject=JsonArray.getJSONObject(i);

jsonObjectArray=new String[key.length];

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

jsonArrayList.add(jsonObjectArray);
}

return jsonArrayList;

}catch(Exception e){

e.printStackTrace();
return null;
}
}

//========================================================================
/**
* <p>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
* <ul><li>First row : 1st element- value00</li>
* <li>Second row : 1st element- value10</li></ul>
* </p>
*
* */
//========================================================================
public static ArrayList<String[]> convertJSONStringtoArrayList(String jsonString,String[] key){

try{

JSONObject jsonObject=new JSONObject(jsonString);

ArrayList<String[]> jsonArrayList=new ArrayList<String[]>();

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

return jsonArrayList;

}catch(Exception e){

e.printStackTrace();
return null;
}
}

}。
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
day忘不掉的痛
2015-02-06 · 知道合伙人数码行家
day忘不掉的痛
知道合伙人数码行家
采纳数:62646 获赞数:223939
本人担任公司网络部总经理多年,有充足的网络经验、互联网相关知识和资讯。

向TA提问 私信TA
展开全部
您好,很高兴为您解答,方法好下:
import java.util.ArrayList;

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

import android.util.Log;

public class JSON{

//========================================================================
/**
* <b><p> retrieveJSONArray(ArrayList<</String[]>> jsonArray)</b></p>
*
* <ul><li>Returns JSON formed Array from the ArrayList provided.</li>
* <li><b>jsonArray</b> will be ArrayList of array.</li>
* <li>the elements provided in array will be arranged in consecutive keys</li>
* <li>ex:<b> [{"key0","1st element of array"},{"key1","2nd element of array"}]</b> </li>
* </ul>
*/
//========================================================================
public static String retrieveJSONArray(ArrayList<String[]> jsonArray){

try{

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

stringer.array();

int arrayLength=jsonArray.size();

for(int i=0;i<arrayLength;i++){

jsonObject=jsonArray.get(i);

stringer.object();

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

stringer.endObject();
}

stringer.endArray();

return stringer.toString();

}catch(Exception e){

e.printStackTrace();
}
return null;
}

//========================================================================
/**
* <b><p> retrieveJSONArray(ArrayList<</String[]>> jsonArray,String[] key)</b></p>
*
* <ul><li>Returns JSON formed Array from the ArrayList provided.</li>
* <li><b>jsonArray</b> will be ArrayList of array.</li>
* <li>the elements provided in array will be arranged in consecutive keys</li>
* <li>ex:<b> [{"key[0]","1st element of array"},{"key[1]","2nd element of array"}]</b> </li>
* </ul>
*/
//========================================================================
public static String retrieveJSONArray(ArrayList<String[]> jsonArray,String[] key){

try{

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

stringer.array();

int arrayLength=jsonArray.size();

for(int i=0;i<arrayLength;i++){

jsonObject=jsonArray.get(i);

stringer.object();

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

stringer.endObject();
}

stringer.endArray();

return stringer.toString();

}catch(Exception e){

e.printStackTrace();
}
return null;
}

//========================================================================
/**
* <b><p> retrieveJSONString(ArrayList<</String[]>> jsonArray)</b></p>
*
* <ul><li>Returns JSON formed string from the ArrayList provided.</li>
* <li><b>jsonArray</b> will be ArrayList of array.</li>
* <li>ex:<b> {"key0":"1st element of array","key1":"2nd element of array"}</b> </li>
* </ul>
*/
//========================================================================
public static String retrieveJSONString(ArrayList<String[]> jsonObject){

try{

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

stringer.object();

for(int i=0;i<jsonObject.size();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;
}

//========================================================================
/**
* <p>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
* <ul><li>First row : 1st element- value00, 2nd element - value01</li>
* <li>Second row : 1st element- value10, 2nd element - value11</li></ul>
* </p>
*
* */
//========================================================================
public static ArrayList<String[]> convertJSONArraytoArrayList(String jsonArray,String[] key){

try{

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

int jsonArraySize=JsonArray.length();

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

for(int i=0;i<jsonArraySize;i++){

JsonObject=JsonArray.getJSONObject(i);

jsonObjectArray=new String[key.length];

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

jsonArrayList.add(jsonObjectArray);
}

return jsonArrayList;

}catch(Exception e){

e.printStackTrace();
return null;
}
}

//========================================================================
/**
* <p>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
* <ul><li>First row : 1st element- value00</li>
* <li>Second row : 1st element- value10</li></ul>
* </p>
*
* */
//========================================================================
public static ArrayList<String[]> convertJSONStringtoArrayList(String jsonString,String[] key){

try{

JSONObject jsonObject=new JSONObject(jsonString);

ArrayList<String[]> jsonArrayList=new ArrayList<String[]>();

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

return jsonArrayList;

}catch(Exception e){

e.printStackTrace();
return null;
}
}

}
如果我的回答没能帮助您,请继续追问。
转载,仅供参考。
本回答被提问者和网友采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 1条折叠回答
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式