怎样将json对象数组转换为字符串数组

怎样将json对象数组转换为字符串数组,第1张

将json字符串转成Java的Array数组
private String json = "{\"address\":\"chian\",\"birthday\":{\"birthday\":\"2010-11-22\"},"+
"\"email\":\"email@123com\",\"id\":22,\"name\":\"tom\"}";
@Test
public void readJSON2Array() {
try {
json = "[" + json + "]";
jsonArray = JSONArrayfromObject(json);
Object[] os = jsonArraytoArray();
Systemoutprintln(oslength);
Student[] stus = (Student[]) JSONArraytoArray(jsonArray, Studentclass);
Systemoutprintln(stuslength);
Systemoutprintln(stus[0]);
} catch (Exception e) {
eprintStackTrace();
}
}
运行的结果如下:
==============JSON Arry String >>> Java Array ==================
#%%%{"address":"chian","birthday":{"birthday":"2010-11-22"},"email":"email@123com","id":22,"name":"tom"}
1
{"address":"chian","birthday":{"birthday":"2010-11-22"},"email":"email@123com","id":22,"name":"tom"}
{"address":"chian","birthday":{"birthday":"2010-11-22"},"email":"email@123com","id":22,"name":"tom"}
1
  将JSON字符串转成Java的List集合
private String json = "{\"address\":\"chian\",\"birthday\":{\"birthday\":\"2010-11-22\"},"+
"\"email\":\"email@123com\",\"id\":22,\"name\":\"tom\"}";
public void readJSON2List() {
try {
json = "[" + json + "]";
jsonArray = JSONArrayfromObject(json);
List<Student> list = JSONArraytoList(jsonArray, Studentclass);
Systemoutprintln(listsize());
Systemoutprintln(listget(0));
list = JSONArraytoList(jsonArray);
Systemoutprintln(listsize());
Systemoutprintln(listget(0));
} catch (Exception e) {
eprintStackTrace();
}
}

在数据传输过程中,json是以文本,即字符串的形式传递的,而JS *** 作的是JSON对象,所以,JSON对象和JSON字符串之间的相互转换是关键。例如: JSON字符串: var str1 = '{ "name": "cxh", "sex": "man" }'; JSON对象: var str2 = { "name": "cxh

通常是下载一个叫json2js的脚本文件引入系统中,在这个js中,提供了json的序列化和反序列化方法,可以将一个json对象转换成json字符串,也可以将一个json字符串转换成一个json对象。JSONparse()--字符串转换json、JSONstringify()--json转换成字符串

需要导入两个jar包 json-lib是用于转换json字符串的核心jar包,上面那个是辅助的。 转换json数组就是JSONArrayfromObject(arrayList)toString(); 转换json对象就是JSONObjectfromObject(arrayList)toString();


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/yw/13328984.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-07-15
下一篇 2023-07-15

发表评论

登录后才能评论

评论列表(0条)

保存