- 1、java怎麼讀取json格式的數據
- 2、JAVA怎麼取JSON數組裡的值???
- 3、java json數組遍歷層如下結果,跪求大神
- 4、JAVA中json讀取數組的方法
- 5、跪求大神,用js或者java循環遍歷json數組,實現下面功能,太難了,實在不會,跪求了(6)。
java可以使用JSONObject和JSONArray來操作json對象和json數組,具體用法如下
1:java對象與json串轉換:
java對象—json串:
JSONObject JSONStr = JSONObject.fromObject(object);
String str = JSONStr.toString();
json串—java對象:
JSONObject jsonObject = JSONObject.fromObject( jsonString );
Object pojo = JSONObject.toBean(jsonObject,pojoCalss);
2:java數組對象與json串轉換:
java數組—json串:
JSONArray arrayStr = JSONArray.fromObject(List?);
String str = arrayStr.toString();
json串—java數組:
JSONArray array = JSONArray.fromObject(str);
List? list = JSONArray.toList(array, ?.class);
需要寫一個方法,把json數據轉換成list集合數據
public static List jsonToBean(String data, Object bean) {
List list = new ArrayList();
try {
JSONArray array;
try {
array = new JSONArray(data);
for (int i = 0; i array.length(); i++) {
Object toBean = getBean(bean);
JSONObject ob = new JSONObject();
ob = (JSONObject) array.get(i);
toBean = jsonStrToBean(ob, toBean);
list.add(toBean);
}
return list;
} catch (JSONException e) {
try {
Object obj = null;
JSONObject jsonObj = new JSONObject(data);
Object toBean = getBean(bean);
toBean = jsonStrToBean(jsonObj, toBean);
list.add(toBean);
return list;
} catch (JSONException e1) {
log.error(“Error covert String to JSONObject”, e);
e1.printStackTrace();
}
e.printStackTrace();
log.error(“Error covert String to JSONArray”, e);
}
} catch (SecurityException e) {
e.printStackTrace();
}
return list;
}
然後取出list集合數據中的值應該沒問題了吧。
!DOCTYPE HTML
html lang=”en-US”
head
meta charset=”UTF-8″
titlemenu/title
style type=”text/css”
/style
/script
script type=”text/javascript”
var json = [
{
“index”: “name”,
“id”: 4,
“colspan”: 1,
“sort”: 0,
“title”: “你是”,
“age”: 40
},
{
“id”: 4,
“colspan”: 3,
“sort”: 1,
“title”: “我”,
“age”: 40
},
{
“id”: 4,
“colspan”: 15,
“sort”: 2,
“title”: “的”,
“age”: 40
},
{
“index”: “a”,
“id”: 3,
“colspan”: 1,
“sort”: 0,
“title”: “人”,
“age”: 20
},
{
“index”: “b”,
“id”: 3,
“colspan”: 1,
“sort”: 1,
“title”: “好”,
“age”: 20
},
{
“id”: 3,
“colspan”: 5,
“sort”: 2,
“title”: “馬”,
“age”: 40
},
{
“id”: 2,
“colspan”: 2,
“sort”: 0,
“title”: “上”,
“age”: 40
},
{
“id”: 2,
“colspan”: 2,
“sort”: 1,
“title”: “就”,
“age”: 40
},
{
“index”: “f”,
“id”: 2,
“colspan”: 1,
“sort”: 2,
“title”: “額”,
“age”: 25
},
{
“index”: “c”,
“id”: 1,
“colspan”: 1,
“sort”: 0,
“title”: “一”,
“age”: 30
},
{
“index”: “d”,
“id”: 1,
“colspan”: 1,
“sort”: 1,
“title”: “個”,
“age”: 25
}
];
var array = [], obj = {};
for (var i = 0; i json.length; i++)
{
var temp = obj[json[i].id];
if (!temp)
{
obj[json[i].id] = [
json[i]
];
}
else if (temp.length 0)
{
temp.push(json[i]);
temp.sort(function(a, b)
{
var x = parseFloat(a[“sort”], 10), y = parseFloat(b[“sort”], 10);
if (x y)
{
return 1;
}
else if (x y)
{
return -1;
}
else
{
return 0;
}
});
}
}
for (var p in obj)
{
array.push(obj[p]);
}
array.sort(function(a, b)
{
var x = parseFloat(a[0].id, 10), y = parseFloat(b[0].id, 10);
if (x y)
{
return -1;
}
else if (x y)
{
return 1;
}
else
{
return 0;
}
});
console.log(array)
/script
/head
body
/body
/html
Java語言以JSON的官方庫和JSON-lib庫來寫
取likes是
String t=轉義後的{“initial”:”R”,”name”:”reiz”,”likes”:[“JavaScript”,”Skiing”,”Apple Pie”]}
JSONObject jo=new JSONObject(t); //json-lib中是jo=JSONObject.fromObject(t);
JSONArray likes=jo.getJSONArray(“likes”); //得到likes數組
System.out.prinlnt(like.getString(2)); //打印Apple Pie
var origin = [
{“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}
];
var finalData = []; // 最終的數據
transferData(); // 數據轉換
console.log(finalData, JSON.stringify(finalData));
function transferData() {
origin.forEach(function(n) {
var first = getRecordById(n.first_id, finalData);
if (first) {
first.age += n.age;
first.parent += n.parent;
first.people += n.people;
var second = getRecordById(n.second_id, first.children);
if (second) {
second.age += n.age;
second.parent += n.parent;
second.people += n.people;
var third = 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
}]
}]
});
}
});
}
function getRecordById(id, data) {
for (var i = 0, n = data.length; i n; i++) {
if (data[i].id == id) return data[i];
}
return null;
}
原創文章,作者:OJVD5,如若轉載,請註明出處:https://www.506064.com/zh-hk/n/126835.html