猿问
下载APP

求教各位大侠,如何使用Java语言遍历这段Json数组的每个元素,因为结构比较复杂,有点无从下手,求代码示例

[
    {
        "projectName": "bingo-paas",
        "appName": "bingo-devops",
        "progType": "GENERAL",
        "deployType": "CONTAINER",
        "projectId": "p-19sn3n1sv66c0",
        "progId": "g-19sn59o2m29e0",
        "progName": "bingo-devops-console",
        "packs": [
            {
                "packId": "k-19ssm5j5dc220",
                "packCode": "bingo-devops-console",
                "packUri": "registry.bingosoft.net/bingodevops/bingo-devops-console",
                "packVersion": "latest",
                "repo": {
                    "repoId": "r-1adnr9sbnshbo",
                    "repoName": "registry",
                    "repoType": "CONTAINER",
                    "repoUri": "http://registry.bingosoft.net",
                    "handler": {},
                    "projectName": "bingo-paas",
                    "credentialId": "d-1adnr9pusbmkp",
                    "repoServer": "http://registry.bingosoft.net",
                    "hibernateLazyInitializer": {},
                    "projectId": "p-19sn3n1sv66c0",
                    "credentialName": "paas_app_ecr"
                },
                "progId": "g-19sn59o2m29e0",
                "appId": "a-19sn3okvsq0i0"
            }
        ],
        "appId": "a-19sn3okvsq0i0",
        "createdOn": "2018-02-25 02:21:57",
        "createdBy": "308b29c4-3f5b-4a3d-9d5b-4e41ae4dda81",
        "createdByName": "李强"
    },
    {
        "projectName": "bingo-paas",
        "appName": "bingo-devops",
        "progType": "GENERAL",
        "deployType": "CONTAINER",
        "projectId": "p-19sn3n1sv66c0",
        "progId": "g-19ta9di7jmpq0",
        "progName": "bingo-devops-agent",
        "packs": [
            {
                "packId": "gp-19tmlpje1inrc",
                "packCode": "bingo-devops-agent",
                "packUri": "registry.bingosoft.net/bingodevops/bingo-devops-agent",
                "packVersion": "latest",
                "repo": {
                    "repoId": "r-1adnr9sbnshbo",
                    "repoName": "registry",
                    "repoType": "CONTAINER",
                    "repoUri": "http://registry.bingosoft.net",
                    "handler": {},
                    "projectName": "bingo-paas",
                    "credentialId": "d-1adnr9pusbmkp",
                    "repoServer": "http://registry.bingosoft.net",
                    "hibernateLazyInitializer": {},
                    "projectId": "p-19sn3n1sv66c0",
                    "credentialName": "paas_app_ecr"
                },
                "progId": "g-19ta9di7jmpq0",
                "appId": "a-19sn3okvsq0i0"
            }
        ],
        "appId": "a-19sn3okvsq0i0",
        "createdOn": "2018-02-25 02:21:57",
        "createdBy": "308b29c4-3f5b-4a3d-9d5b-4e41ae4dda81",
        "createdByName": "李强"
    }
]


BillyWebber
浏览 416回答 1
1回答

pardon110

需要用到json相关的jar包,可以用库的,也可以用阿里的fastjson遍历json数组简单示例如下// 一个未转化的字符串 String str = "[{name:'a',value:'aa'},{name:'b',value:'bb'},{name:'c',value:'cc'},{name:'d',value:'dd'}]" ;   // 首先把字符串转成 JSONArray  对象 JSONArray json = JSONArray.fromObject(str ); if(json.size()>0){   for(int i=0;i<json.size();i++){  // 遍历 jsonarray 数组,把每一个对象转成 json 对象 JSONObject job = json.getJSONObject(i);  // 得到 每个对象中的属性值 System.out.println(job.get("name")+"=") ;     }
打开App,查看更多内容
随时随地看视频慕课网APP
我要回答