java遍历json
⑴ java json遍历问题,新手。
步骤一:导入jar
json-lib-2.2.2-jdk15.jar
json-lib依赖包:commons-lang.jar commons-beanutils.jar commons-collections.jar commons-logging.jar ezmorph.jar
步骤二:建对应的实体类
public classRegion(){
Stringcategory;
StringcityName;
StringsiteName;
Stringtotal;
//省略get/set
}
步骤三:遍历
public class Test {
//将json字符串转List
publicstaticList<Region>converListFormJson(String json){
if(json==null||json.equals("")) {
returnnewArrayList();
}
JSONArrayjsonArray=JSONArray.fromObject(json);
List<Region>list=(List)JSONArray.toCollection(jsonArray, Region.class);
returnlist;
}
public static void main(String args[]){
List<Region> list = converListFormJson();
for(Region region : list){ //遍历所有的
System.out.println("...............业务............");
}
}
}
⑵ java如何遍历json 请具体点
json是字符串,java 没有提供API遍历json吧,要借助jar包,也就是第三方的API,对json格式的字符串或版是java对象之间权的转换,比如:
一个json格式的字符串,用第三方API(比如gson或jackjson)对json格式字符串进行转换为List类型的,然后用for循环对这list 遍历输出即可,其它对象也是可以的,自定义的实体也行,gson或jackjson有对应的方法能做到
⑶ JAVA中按原顺序遍历JSONObject
正常的玩法应该是这样, 因为 ObjectMapper 有各种配置, 可以更好的实现转换, 比如 解析数据顺序版, 忽略错误字段权 等等 高端大气上档次的东西, 你的方法已经被时代淘汰了。
ObjectMapper objectMapper = new ObjectMapper();
Map map = objectMapper.readValue(resultStr,LinkedHashMap.class);
⑷ java foreach是否能对jsonarray进行遍历
java foreach能对jsonarray进行遍历。foreach 语句为数组或对象集合中的每个元素重复回一个嵌入语句组。foreach 语句用于循环访问集合以答获取所需信息,但不应用于更改集合内容以避免产生不可预知的副作用。
拓展:
1、Java是一种可以撰写跨平台应用软件的面向对象的程序设计语言。Java 技术具有卓越的通用性、高效性、平台移植性和安全性,广泛应用于PC、数据中心、游戏控制台、科学超级计算机、移动电话和互联网,同时拥有全球最大的开发者专业社群。
2、Java是由Sun Microsystems公司推出的Java面向对象程序设计语言(以下简称Java语言)和Java平台的总称。由James Gosling和同事们共同研发,并在1995年正式推出。Java最初被称为Oak,是1991年为消费类电子产品的嵌入式芯片而设计的。1995年更名为Java,并重新设计用于开发Internet应用程序。
⑸ js或者Java 遍历json数组 实现如下功能 跪求大神
/**
*json格式转树状结构
*@param {json} json数据
*@param {String} id的字符串
*@param {String} 父id的字符串
*@param {String} children的字符串
*@return {Array} 数组
*/
functiontransData(a,idStr,pidStr,chindrenStr){
varr=[],hash={},id=idStr,pid=pidStr,children=chindrenStr,i=0,j=0,len=a.length;
for(;i<len;i++){
hash[a[i][id]]=a[i];
}
for(;j<len;j++){
varaVal=a[j],hashVP=hash[aVal[pid]];
if(hashVP){
!hashVP[children]&&(hashVP[children]=[]);
hashVP[children].push(aVal);
}else{
r.push(aVal);
}
}
returnr;
}
以上方法完全可以满足你的 需求,Json属性结构的转换。
调用方法:
vara=[{
"a":"5",
"id":"0000",
"parent_id":"null"
},{
"a":"1",
"id":"1000",
"parent_id":"0000"
},{
"a":"2",
"id":"2000",
"parent_id":"0000"
},{
"a":"6",
"id":"1001",
"parent_id":"1000"
},{
"a":"6",
"id":"1002",
"parent_id":"1000"
},{
"a":"23",
"id":"2001",
"parent_id":"2000"
},{
"a":"11",
"id":"2002",
"parent_id":"2000"
}];
varb=transData(a,"id","parent_id","children");
那么b就是你需要的结构。transData方法在以后遇到类似结构的数据,都可以使用!
以上的所有方法都是JavaScript !
⑹ Java实现JSON多层遍历
JSONObject jsonObject = new JSONObject(s);
然后用Iterator迭代器遍历取值,建议用反射机制解析到专封装好的对象属中
JSONObject jsonObject = new JSONObject(jsonString);
Iterator iterator = jsonObject.keys();while(iterator.hasNext()){
key = (String) iterator.next();
value = jsonObject.getString(key);
}
⑺ java 遍历json数组
现写了一个,本地测试完全满足你的要求,现把源代码和用到的jar包传为附件。有什么不会用的再追问。
⑻ java foreach是否能对jsonarray进行遍历
应该是可以的
1.我用的jar包是com.alibaba.fastjson是这个
2.代码片段
1
2
3
4
5
JSONArray arrays = JSONObject.parseArray("集合json");
for(Object obj:arrays){
JSONObject o = (JSONObject) obj;
String value = o.getString("JSONArray每个元素中,你需要的那个key");
}
⑼ java怎么遍历json取出需要的内容
java遍历json的话,你可以选择gson或者阿里巴巴的fastjson,这两个解析json的话是非常方便的,可以转换成java对象。
⑽ 跪求大神,用js或者java循环遍历json数组,实现下面功能,太难了,实在不会,跪求了(6)。
varorigin=[
{"first_id":1,"first_name":"中学","second_id":"1-1","second_name":"一年级","third_id":"1-1-1","third_name":"一年级一班","people":10,"age":10,"parent":5},
{"first_id":1,"first_name":"中学","second_id":"1-1","second_name":"一年级","third_id":"1-1-2","third_name":"一年级二班","people":11,"age":10,"parent":5},
{"first_id":1,"first_name":"中学","second_id":"1-2","second_name":"二年级","third_id":"1-2-1","third_name":"二年级一班","people":20,"age":10,"parent":5},
{"first_id":1,"first_name":"中学","second_id":"1-2","second_name":"二年级","third_id":"1-2-2","third_name":"二年级二班","people":21,"age":10,"parent":5},
{"first_id":2,"first_name":"高中","second_id":"2-1","second_name":"一年级","third_id":"2-1-1","third_name":"一年级一班","people":31,"age":10,"parent":5}
];
varfinalData=[];//最终的数据
transferData();//数据转换
console.log(finalData,JSON.stringify(finalData));
functiontransferData(){
origin.forEach(function(n){
varfirst=getRecordById(n.first_id,finalData);
if(first){
first.age+=n.age;
first.parent+=n.parent;
first.people+=n.people;
varsecond=getRecordById(n.second_id,first.children);
if(second){
second.age+=n.age;
second.parent+=n.parent;
second.people+=n.people;
varthird=getRecordById(n.third_id,second.children);
if(third){
//这里应该不会存在
}else{
second.children.push({
id:n.third_id,
name:n.third_name,
age:n.age,
parent:n.parent,
people:n.people
});
}
}else{
first.children.push({
id:n.second_id,
name:n.second_name,
age:n.age,
parent:n.parent,
people:n.people,
children:[{
id:n.third_id,
name:n.third_name,
age:n.age,
parent:n.parent,
people:n.people
}]
});
}
}else{
finalData.push({
id:n.first_id,
name:n.first_name,
age:n.age,
parent:n.parent,
people:n.people,
children:[{
id:n.second_id,
name:n.second_name,
age:n.age,
parent:n.parent,
people:n.people,
children:[{
id:n.third_id,
name:n.third_name,
age:n.age,
parent:n.parent,
people:n.people
}]
}]
});
}
});
}
functiongetRecordById(id,data){
for(vari=0,n=data.length;i<n;i++){
if(data[i].id==id)returndata[i];
}
returnnull;
}