java循环获取json数组(如何获取json中的数组)

  • 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/n/126835.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
OJVD5OJVD5
上一篇 2024-10-03 23:13
下一篇 2024-10-03 23:13

相关推荐

  • Java JsonPath 效率优化指南

    本篇文章将深入探讨Java JsonPath的效率问题,并提供一些优化方案。 一、JsonPath 简介 JsonPath是一个可用于从JSON数据中获取信息的库。它提供了一种DS…

    编程 2025-04-29
  • java client.getacsresponse 编译报错解决方法

    java client.getacsresponse 编译报错是Java编程过程中常见的错误,常见的原因是代码的语法错误、类库依赖问题和编译环境的配置问题。下面将从多个方面进行分析…

    编程 2025-04-29
  • Java腾讯云音视频对接

    本文旨在从多个方面详细阐述Java腾讯云音视频对接,提供完整的代码示例。 一、腾讯云音视频介绍 腾讯云音视频服务(Cloud Tencent Real-Time Communica…

    编程 2025-04-29
  • Java Bean加载过程

    Java Bean加载过程涉及到类加载器、反射机制和Java虚拟机的执行过程。在本文中,将从这三个方面详细阐述Java Bean加载的过程。 一、类加载器 类加载器是Java虚拟机…

    编程 2025-04-29
  • Java Milvus SearchParam withoutFields用法介绍

    本文将详细介绍Java Milvus SearchParam withoutFields的相关知识和用法。 一、什么是Java Milvus SearchParam without…

    编程 2025-04-29
  • Python导入数组

    本文将为您详细阐述Python导入数组的方法、优势、适用场景等方面,并附上代码示例。 一、numpy库的使用 numpy是Python中一个强大的数学库,其中提供了非常丰富的数学函…

    编程 2025-04-29
  • Java 8中某一周的周一

    Java 8是Java语言中的一个版本,于2014年3月18日发布。本文将从多个方面对Java 8中某一周的周一进行详细的阐述。 一、数组处理 Java 8新特性之一是Stream…

    编程 2025-04-29
  • Python返回数组:一次性搞定多种数据类型

    Python是一种多用途的高级编程语言,具有高效性和易读性的特点,因此被广泛应用于数据科学、机器学习、Web开发、游戏开发等各个领域。其中,Python返回数组也是一项非常强大的功…

    编程 2025-04-29
  • Java判断字符串是否存在多个

    本文将从以下几个方面详细阐述如何使用Java判断一个字符串中是否存在多个指定字符: 一、字符串遍历 字符串是Java编程中非常重要的一种数据类型。要判断字符串中是否存在多个指定字符…

    编程 2025-04-29
  • VSCode为什么无法运行Java

    解答:VSCode无法运行Java是因为默认情况下,VSCode并没有集成Java运行环境,需要手动添加Java运行环境或安装相关插件才能实现Java代码的编写、调试和运行。 一、…

    编程 2025-04-29

发表回复

登录后才能评论