本文目录一览:
- 1、iOS 怎么用post方式上传json数据
- 2、iosafn怎么用post上传json数据
- 3、ios以post请求传来的json 在java怎么获取??
- 4、2020-07-30 ios post请求方式表单与Json
- 5、iOS post请求向服务器发送json格式数据(数组或字典)
iOS 怎么用post方式上传json数据
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;iarrayLength;i++){
jsonObject=jsonArray.get(i);
stringer.object();
for(int j=0;jjsonObject.length;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;iarrayLength;i++){
jsonObject=jsonArray.get(i);
stringer.object();
for(int j=0;jjsonObject.length;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;ijsonObject.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;
}
//========================================================================
/**
*
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;ijsonArraySize;i++){
JsonObject=JsonArray.getJSONObject(i);
jsonObjectArray=new String[key.length];
for(int j=0;jkey.length;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;ikey.length;i++)
jsonArrayList.add(new String[]{jsonObject.getString(key[i])});
return jsonArrayList;
}catch(Exception e){
e.printStackTrace();
return null;
}
}
}
x
iosafn怎么用post上传json数据
代码如下:
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
//申明返回的结果是json类型
manager.responseSerializer = [AFJSONResponseSerializer serializer];
//申明请求的数据是json类型
manager.requestSerializer=[AFJSONRequestSerializer serializer];
//如果报接受类型不一致请替换一致text/html或别的
manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"];
//传入的参数
NSDictionary *parameters = @{@"1":@"XXXX",@"2":@"XXXX",@"3":@"XXXXX"};
//你的接口地址
NSString *url=@"http://";
//发送请求
[manager POST:url parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"JSON: %@", responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Error: %@", error);
ios以post请求传来的json 在java怎么获取??
MapString, String[] paramMap = request.getParameterMap();
String data = paramMap.get("param")[0];//这里要看它是怎么放的。
ObjectMapper mapper = new ObjectMapper();
MapString, Object map=mapper.readValue(data , Map.class);
2020-07-30 ios post请求方式表单与Json
最近遇到的坑,两个后台,一个表单,一个Json格式,之前一直没搞明白
附上源码供参考
iOS post请求向服务器发送json格式数据(数组或字典)
1.字典
(ps usernames 是字段,jsonString是转换json格式的字典)
2.数组
数组其实和字典一样,只需将(dataWithJSONObject: dicFriends )参数,换成数组就可以了