/**
* 用json发送数据 如int [] ,intjble[] ,String [] ....
* 这里是封装并发送 ,方法略显笨拙
*/
@Test
public void jsonSendArray(){
int [] intj={1,2,3,4,5,6,7,8,9,0};
JSONObject clientKey = new JSONObject();
JSONArray jsonArray = new JSONArray(); //保存数组数据的JSONArray对象
for(int i = 0 ; i < intj.length ;i++){ //依次将数组元素添加进JSONArray对象中
jsonArray.put(intj[i]);
// 两种添加方式一样
// try {
// jsonArray.put(i, intj[i]);
// } catch (JSONException e) {
// e.printStackTrace();
// }
}
try {
clientKey.put("intArray", jsonArray);//jsonArray相当于int数组
System.out.println(clientKey.toString()); //输出测试
} catch (JSONException e) {
e.printStackTrace();
}
}
/**
* JSONObject 和JSONArray的一些基础用法
*/
//json字符串转jsonObject,
@Test
public void string2json(){
String str="{\"name\":\"Iyangc\",\"age\":21,\"sex\":'男',\"address\":{\"province\":\"四川\",\"city\":\"成都\"}}";
try {
JSONObject jsonObject = new JSONObject(str);
String name = jsonObject.getString("name");
JSONObject address = jsonObject.getJSONObject("address");
String province = address.getString("province");
int age = jsonObject.getInt("age");
System.out.println("name: "+name+"\nprovince: "+province+"\nage: "+age);
} catch (JSONException e) {
e.printStackTrace();
}
}
//json转Jsonarray, 多组数据,json字符串的格式。
@Test
public void json2String(){
String str= "[{\"name\":\"刑名\",\"age\":22,\"address\":" +
"{\"province\":\"四川\",\"city\":\"成都\",\"details\":\"光明街16号\"}}," +
"{\"name\":\"房恰\",\"age\":23,\"address\":" +
"{\"province\":\"四川\",\"city\":\"成都\",\"details\":\"建设路5号\"}}," +
"{\"name\":\"刘海\",\"age\":23,\"address\":" +
"{\"province\":\"四川\",\"city\":\"成都\",\"details\":\"中山南路8号\"}}]";
try {
JSONArray jsonArray = new JSONArray(str); //将String转换成JsonArray对象
int length=jsonArray.length();
//取出数据
for (int i = 0; i < length; i++) {
// System.out.println(jsonArray.getJSONObject(i));//输出每一组数据
JSONObject jsonObject = jsonArray.getJSONObject(i);
String name = jsonObject.getString("name");
JSONObject jsonObject2 = jsonObject.getJSONObject("address");
System.out.println(jsonObject2);//输出每一组数据的address
String province = jsonObject2.getString("province");//每一组address中的province
System.out.println(name+"-----------"+province);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
/**
*
* */
//以键值对写入json
@Test
public void jsonArray2String(){
JSONArray jsonArray = new JSONArray();
JSONObject jsonObject1= new JSONObject();
JSONObject jsonObject2= new JSONObject();
JSONObject jsonObject3= new JSONObject();
try {
jsonObject1 .put("name","smile");
jsonObject1 .put("age",21);
JSONObject addressObject1 = new JSONObject();
addressObject1.put("province", "四川");
jsonObject1.put("address",addressObject1 );
jsonObject2 .put("name","金士顿");
jsonObject2 .put("age",22);
JSONObject addressObject2 = new JSONObject();
addressObject2.put("province", "四川简阳");
jsonObject2.put("address",addressObject2 );
jsonObject3 .put("name","酸豆角");
jsonObject3 .put("age",21);
JSONObject addressObject3 = new JSONObject();
addressObject3.put("province", "四川石桥");
jsonObject3.put("address",addressObject3 );
jsonArray.put(jsonObject1);
jsonArray.put(jsonObject2);
jsonArray.put(jsonObject3);
//打印输出转换成json格式后的String字符串
String string = jsonArray.toString();
System.out.println(string );
} catch (JSONException e) {
e.printStackTrace();
}
}