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

如何把json字符串转化为list 如何将json类型的字符串转换成list

2023-04-25 15:36:10 互联网 未知 开发

 如何把json字符串转化为list 如何将json类型的字符串转换成list

如何把json字符串转化为list

List list = new ArrayList()
list.add("测试1")
list.add("测试2")
list.add("测试3")
JSONArray jsonArray = JSONArray.fromObject(list)
System.out.println(jsonArray.toString())
这边要导包需要用到json-lib.jar, net.sf.json.JSONArray

如何将json类型的字符串转换成list

String list = request.getParameter("json")
JSONArray data = JSONArray.fromObject(list)
for(int i=0iJSONObject jobj = (JSONObject) data.get(i) String name = jobj.get("name") }

怎么把json string数据转换成list集合

将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()

Json格式的数据怎么转换成List

JSONObject.toBean

String list = request.getParameter("json")
JSONArray data = JSONArray.fromObject(list)
for(int i=0i JSONObject jobj = (JSONObject) data.get(i)
String name = jobj.get("name")
}
以上代码就是从json数组装换的。

JSON字符串怎么转List和map集合

package jsonToMap

import java.util.List
import java.util.Map
import java.util.Map.Entry

import net.sf.json.JSONArray
import net.sf.json.JSONObject

/**
 * 说明 json字符串 转成 Map/List
 */
public class JsonToMap {

public static void main(String[] args) {

//JSONArray
String jsonArrayData="[{"a1":"12","b1":"112","c1":"132","d1":"134"},{"a2":"12","b2":"112","c2":"132","d2":"134"},{"a3":"12","b3":"112","c3":"132","d3":"134"}]"
JSONArray jsonArray = JSONArray.fromObject(jsonArrayData)

List> mapListJson = (List)jsonArray
for (int i = 0 i < mapListJson.size() i ) {
Map obj=mapListJson.get(i)

for(Entry entry : obj.entrySet()){
            String strkey1 = entry.getKey()
            Object strval1 = entry.getValue()
            System.out.println("KEY:" strkey1 "  -->  Value:" strval1 " ")
        }
}

// JSONObject 
String jsonObjectData="{"data1":{"a1":"12","b1":"112","c1":"132","d1":"134"},"data2":{"a2":"12","b2":"112","c2":"132","d2":"134"},"data3":{"a3":"12","b3":"112","c3":"132","d3":"134"}}"
JSONObject jsonObject = JSONObject.fromObject(jsonObjectData)

Map mapJson = JSONObject.fromObject(jsonObject)

        for(Entry entry : mapJson.entrySet()){
            Object strval1 = entry.getValue()
            JSONObject jsonObjectStrval1 = JSONObject.fromObject(strval1)
            Map mapJsonObjectStrval1 = JSONObject.fromObject(jsonObjectStrval1)
            System.out.println("KEY:" entry.getKey() "  -->  Value:" entry.getValue() " ")
            for(Entry entry1:mapJsonObjectStrval1.entrySet()){
             System.out.println("KEY:" entry1.getKey() "  -->  Value:" entry1.getValue() " ")
            }

        }
}

}