作者:禎冬魔_784 | 来源:互联网 | 2023-09-18 17:17
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 个解决方案