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

json中有数组java怎么解析 java解析json数据成数组

2023-05-30 20:37:00 互联网 未知 开发

 json中有数组java怎么解析 java解析json数据成数组

json中有数组java怎么解析

需要导入json包
public class E {
public static void main(String[] args){
String str="{"code":1,"user":[{"id":123,"name":"小白"},{"id":124,"name":"小黑"}]}"
JSONObject json=new JSONObject(str)
System.out.println(json.get("code"))
JSONArray arr=json.getJSONArray("user")
for(int x=0xJSONObject obj=(JSONObject) arr.get(x) System.out.println(obj.get("id")) System.out.println(obj.get("name")) } } } 结果: 1 123 小白 124 小黑

java解析json数据成数组

import net.sf.json.JSONArray
public class TestJson 
{
public static void main(String []args)
{
   String json = "[{"a":"111","b":"222","c":"333"},{"a":"1000","b":"2000","c":"000"},{"a":"999","b":"300","c":"700"}]"
    JSONArray jsonArr = JSONArray.fromObject(json)
    String a[] = new String[jsonArr.size()]
    String b[] = new String[jsonArr.size()]
    String c[] = new String[jsonArr.size()]
    for (int i = 0 i < jsonArr.size() i ) {
    a[i] = jsonArr.getJSONObject(i).getString("a")
    b[i] = jsonArr.getJSONObject(i).getString("b")
    c[i] = jsonArr.getJSONObject(i).getString("c")
}

    for (int i = 0 i < c.length i ) {
System.out.print(a[i] " ")
System.out.print(b[i] " ")
System.out.print(c[i])
System.out.println()
}
     }
}

用java怎么解析Json数据?

import java.util.ArrayList
import java.util.HashMap
import java.util.Iterator
import java.util.Map

import net.sf.ezmorph.bean.MorphDynaBean
import net.sf.json.JSONArray
import net.sf.json.JSONSerializer

public class JSONDemo {

public static final String PREFIX = "index_"
/**
* @param args
*/
public static void main(String[] args) {
Map map = new HashMap()
String str ="[{status: 5,remarks: \xe6\xa3\x80\xe6\xb5\x8b\xe5\xb7\xb2\xe7\xbb\x8f\xe5\xae\x8c\xe6\x88\x90,session: "(1, 9.2.0.1.0, 192.168.177.115, 1521L, ora9, 1, )",vuls: ["(612300200001, 1, , [{values: [version], type: 0}, {values: [版本号], type: 1}])","(612300200002, 1, , [{values: [version], type: 0},{values: [版本号], type: 1},{values: [9.2.0.1.0], type: 2}])"], endTime:123}, 1L, \xe6\xb5\x8b\xe8\xaf\x95\xe6\x89\xab\xe6\x8f\x8f\xe4\xbb\xbb\xe5\x8a\xa1]"

System.out.println("json格式字符串-->" str)
JSONArray array = JSONArray.fromObject(str)
System.out.println("json格式字符串构造json数组元素的个数-->" array.size())
ArrayList list = (ArrayList) JSONSerializer.toJava(array)

int i = 0
for (Object obj : list) {
map.put(PREFIX (i ), obj)
System.out.println("第" i "对象-->" obj)
}
//解析第0个位置
Map bd = new HashMap()
MorphDynaBean bean = (MorphDynaBean) map.get(PREFIX 0)
bd.put("session", bean.get("session"))
bd.put("status", bean.get("status"))
bd.put("remarks", bean.get("remarks"))
bd.put("vuls", bean.get("vuls"))
bd.put("endTime", bean.get("endTime"))
Iterator iter = bd.keySet().iterator()
while (iter.hasNext()){
Object key = iter.next()
Object value = bd.get(key)
System.out.println("MorphDynaBean对象-->key=" key ",value=" value)
}

//解析vuls
ArrayList vuls = (ArrayList) bd.get("vuls")
Map vl = new HashMap()
int j = 0
for (Object obj : vuls) {
vl.put(PREFIX (j ), obj)
System.out.println("解析vuls的第" i "对象-->" obj)
}

}
}

java 解析json字符串

你好:
后台拆分json
private String interactPrizeAll    //json 使用字符串来接收
方法中的代码:
Gson gson = new Gson()
InteractPrize interactPrize =new InteractPrize()
 //gson用泛型 转List数组 多个对象
ListinteractPrizeList = gson.fromJson(interactPrizeAll, new TypeToken>(){}.getType()) //TypeToken,它是gson提供的数据类型转换器,可以支持各种数据集合类型转换 for(int i = 0 i < interactPrizeList.size() i ) { interactPrize = interactPrizeList.get(i)//获取每一个对象 } 这一种方法是转单个对象时使用的 //gson 转对象 单个对象 //interactPrize = gson.fromJso(interactPrizeAll,InteractPrize.class) 这个方法是我后台拼的json往前台传的方法 jsonStrAll.append("{" """ "catid" """ ":" """ c.getCatid() """ "," """ "catname" """ ":" """ c.getCatname() """ "," """ "catdesc" """ ":" """ c.getCatdesc() """ "," """ "showinnav" """ ":" """ c.getShowinnav() """ "," """ "sortorder" """ ":" """ c.getSortorder() """ "," "level:" """ "0" """ ",parent:" """ "0" """ ",isLeaf:true,expanded:false," "loaded:true},")你自己挑着用吧!