本文目录一览:
- 1、java怎么读取json格式的数据
- 2、JAVA怎么取JSON数组里的值???
- 3、java json数组遍历层如下结果,跪求大神
- 4、JAVA中json读取数组的方法
- 5、跪求大神,用js或者java循环遍历json数组,实现下面功能,太难了,实在不会,跪求了(6)。
java怎么读取json格式的数据
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);
JAVA怎么取JSON数组里的值???
需要写一个方法,把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集合数据中的值应该没问题了吧。
java json数组遍历层如下结果,跪求大神
!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读取数组的方法
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
跪求大神,用js或者java循环遍历json数组,实现下面功能,太难了,实在不会,跪求了(6)。
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;
}