热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

在Json数组中搜索值。-SearchingaValueinJsonArray

ihavethefollowingjsonresponse:我有以下json响应:{elements:[{id:1234,

i have the following json response:

我有以下json响应:

{
"elements":
[
    {
        "id": "1234",
        "Key": "1234-name2",  
         "name": "name2",
        "projectName": "TestProject",      
    },
    {
        "id": "5678",
        "applicationKey": "5678-name2",
        "name": "name2",
        "projectName": "TestProject2",
    },
   {
        "id": "9101112",
        "applicationKey": "9101112-name3",
        "name": "name3",
        "projectName": "TestProject3",
    },
],
"totalSize": 3
}

After getting the response, i have converted it to a string:

得到响应后,我将其转换为字符串:

    String PaListCOntent= getContent(PaListResponse);

    private static String getContent(HttpResponse response) {
        HttpEntity entity = response.getEntity();
        if (entity == null) return null;
        BufferedReader reader;
        try {
            reader = new BufferedReader(new InputStreamReader(entity.getContent()));
            String line = reader.readLine();
            reader.close();
            return line;
        } catch (IllegalStateException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

Now, i want to search a projectname (e.g. "Testproject2") in the String and want to have the attributes "id" and "name" as well.

现在,我想搜索一个projectname(例如。“Testproject2”)在字符串中,并希望拥有属性“id”和“name”。

i have tried it with

我试过了

JSONObject jsOnObject= new JSONObject(PaListContent);
JSONObject myRespOnse= jsonObject.getJSONObject("elements");
//JSONArray tsmrespOnse= (JSONArray) myResponse.get("listTsm");

ArrayList list = new ArrayList();

for(int i=0; i

But the problem is, that i always get "org.json.simple.JSONObject cannot be cast to org.json.simple.JSONArray". I think the problem is, because my json is an array, but how can i get the attributes?

但问题是,我总是得到“org.json.simple”。不能将JSONObject强制转换为org.json.simple.JSONArray。我认为问题是,因为我的json是一个数组,但是我如何获得属性呢?

Best Regards!

最好的问候!

2 个解决方案

#1


1  

Your elements is a JSONArray, but you are trying to parse it as a JSONObject, change your code like this:

您的元素是一个JSONArray,但是您试图将它解析为一个JSONObject,可以这样更改代码:

JSONObject jsOnObject= new JSONObject(PaListContent);
JSONArray myRespOnse= jsonObject.getJSONArray("elements");
//JSONArray tsmrespOnse= (JSONArray) myResponse.get("listTsm");

ArrayList list = new ArrayList();

for(int i=0; i

This will work as expected

这将像预期的那样工作

#2


0  

But the problem is, that i always get org.json.simple.JSONObject cannot be cast to org.json.simple.JSONArray. I think the problem is, because my json is an array, but how can i get the attributes?

但问题是,我总是得到org.json.simple。不能将JSONObject强制转换为org.json.simple.JSONArray。我认为问题是,因为我的json是一个数组,但是我如何获得属性呢?

use JSONArray instead

使用获取而不是

JSONArray myRespOnse= jsonObject.getJSONArray("elements");

Here you can find further examples

在这里你可以找到更多的例子


推荐阅读
author-avatar
禎冬魔_784
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有