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;
}