请求报文
{
“Code”: 1024,
“Body”:{
“SessionId”: “666”,
“Uids”: [“1377”,”1288”,”1799”]
}
}
方式一,使用map与list集合
Map<String,Object> hashMap = new HashMap<String,Object>();
Map<String,Object> hashMap2 = new HashMap<String,Object>();
hashMap.put("Code", 1024);
hashMap2.put("SessionId", “666”);
hashMap2.put("Uids", list);//list为传入的字符串list集合List<String>
hashMap.put("Body", hashMap2);
JSONObject fromObject = JSONObject.fromObject(hashMap);
方式二,字符串拼接
String strJson="{\"Code\":1024
,\"Body\":{\"SessionId\":\""+sessionId+"\",\"Uids\":"+uids+"}}";
{
“Result”: 200
“Users”:[
{
“Uid”:”111”,
”Password”:”123456”
},
{
“Uid”:”222”,
”Password”:”123456”
}
]
}
//接口收到的返回body字符串转成了json
JSONObject jsonObject= JSONObject.fromObject(body);
if(!GenUtils.isNull(jsonObject) && jsonObject.getInt("Result") == 200){
JSONArray jsoaArray = jsonObject.getJSONArray("Users");
for(int i =0;i< jsoaArray.size();i++){
JSONObject userJson = (JSONObject) jsoaArray.get(i);
String uid = userJson.getString("Uid");
}
}
public class GenUtils {
public static boolean isNull(Object obj) {
return obj == null || "".equals(obj.toString())
|| "null".equalsIgnoreCase(obj.toString())
|| "undefined".equalsIgnoreCase(obj.toString())
|| "".equals(obj.toString().trim());
}
}