当前位置:首页>开发>正文

如何将一般数组转为json对象数组 怎么将json字符串转成对象数组

2023-04-18 22:46:19 互联网 未知 开发

 如何将一般数组转为json对象数组 怎么将json字符串转成对象数组

如何将一般数组转为json对象数组

将json字符串转成Java的Array数组
private String json = "{"address":"chian","birthday":{"birthday":"2010-11-22"},"
""email":"email@123.com","id":22,"name":"tom"}"

@Test
public void readJSON2Array() {
try {

json = "[" json "]"
jsonArray = JSONArray.fromObject(json)

Object[] os = jsonArray.toArray()
System.out.println(os.length)

Student[] stus = (Student[]) JSONArray.toArray(jsonArray, Student.class)
System.out.println(stus.length)
System.out.println(stus[0])
} catch (Exception e) {
e.printStackTrace()
}
}

运行的结果如下:
==============JSON Arry String >>> Java Array ==================
#%%%{"address":"chian","birthday":{"birthday":"2010-11-22"},"email":"email@123.com","id":22,"name":"tom"}
1
{"address":"chian","birthday":{"birthday":"2010-11-22"},"email":"email@123.com","id":22,"name":"tom"}
{"address":"chian","birthday":{"birthday":"2010-11-22"},"email":"email@123.com","id":22,"name":"tom"}
1

将JSON字符串转成Java的List集合
private String json = "{"address":"chian","birthday":{"birthday":"2010-11-22"},"
""email":"email@123.com","id":22,"name":"tom"}"

public void readJSON2List() {
try {

json = "[" json "]"
jsonArray = JSONArray.fromObject(json)
List list = JSONArray.toList(jsonArray, Student.class)
System.out.println(list.size())
System.out.println(list.get(0))

list = JSONArray.toList(jsonArray)
System.out.println(list.size())
System.out.println(list.get(0))
} catch (Exception e) {
e.printStackTrace()
}
}

怎么将json字符串转成对象数组

//数组转json串
var arr = [1,2,3, { a : 1 } ]
JSON.stringify( arr )

//json字符串转数组
var jsonStr = [1,2,3,{"a":1}]
JSON.parse( jsonStr )

如何将php返回的xml转换成json数据

如果你使用 curl 获取的 xml data
simplexml_load_string() 函数把 XML 字符串载入对象中。
$xml = simplexml_load_string($xmlStr)
$json = json_encode($xml)
如果是直接获取 URL 数据的话
$xml = simplexml_load_file($data)
$json = json_encode($xml)

如何将数组转换成json对象

要使用json来传输数据,必须将所承载的数据转换成json的格式。json的格式如下: JSON建构于两种结构: 1. “名称/值”对的集合。不同的语言中,它被理解为对象(object),纪录(record),结构(struct),字典(dictionary)

最新文章

随便看看