gpt4 book ai didi

java - 如何在 Java 中访问 JSON 数据的嵌套元素?

转载 作者:行者123 更新时间:2023-11-30 07:28:00 24 4
gpt4 key购买 nike

我正在从 URL 获取 JSON 数据,并且想在我的网站上显示该数据。我成功显示了除 JSON 层次结构(JSON 对象)数据之外的所有 JSON 数据。我能够访问 JSONArray personerror 数据。但是,我无法访问层次结构(JSON 对象)更新数据。

我想访问updated.time

import java.io.IOException;
import java.net.URL;
import org.apache.commons.io.IOUtils;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.JSONValue;
import org.json.simple.parser.ParseException;

public class ParseJson1 {

public static void main(String[] args) {
String url = "http://freemusicarchive.org/api/get/genres.json?api_key=60BLHNQCAOUFPIBZ&limit=2";
/*
{
"person": [
{
"name": "John",
"city": "Mumbai"
},
{
"name": "Rahul",
"city": "Delhi"
},
{
"name": "Sanjana",
"city": "Amritsar"
},
{
"name": "Anjali",
"city": "Hyderabad"
},
{
"name": "Mukund",
"city": "Bangalore"
},
{
"name": "Raunak",
"city": "Patna"
}
],
"updated": {
"time": "14:17:48",
"date": "2016-04-10"
},
"error": "2353"
}

*/

try {
String genreJson = IOUtils.toString(new URL(url));
JSONObject genreJsonObject = (JSONObject) JSONValue.parseWithException(genreJson);
// get the error
System.out.println(genreJsonObject.get("error"));

//Get Array Values
JSONArray genreArray = (JSONArray) genreJsonObject.get("person");
// get the first genre
JSONObject firstGenre = (JSONObject) genreArray.get(0);
System.out.println(firstGenre.get("name"));
// get the Second
JSONObject firstGenre = (JSONObject) genreArray.get(1);
System.out.println(firstGenre.get("name"));
// get the third
JSONObject firstGenre = (JSONObject) genreArray.get(2);
System.out.println(firstGenre.get("city"));


} catch (IOException | ParseException e) {
e.printStackTrace();
}
}
}

最佳答案

此 json 结果显示您从 api URL 获得的结果。对吗??

 {  
"person":[
{
"name":"John",
"city":"Mumbai"
},
{
"name":"Rahul",
"city":"Delhi"
},
{
"name":"Sanjana",
"city":"Amritsar"
},
{
"name":"Anjali",
"city":"Hyderabad"
},
{
"name":"Mukund",
"city":"Bangalore"
},
{
"name":"Raunak",
"city":"Patna"
}
],
"updated":{
"time":"14:17:48",
"date":"2016-04-10"
},
"error":"2353"
}

现在这是如何迭代或解析您的 json 对象的代码。我假设上面的结果 json 根据您的代码存储在 String 变量 String GenreJson 中。

Here I wrote a method to solve your Problem. You may take a reference of it and may try your own code.

public void testYourJSON(String genreJson){

JSONParser parser=new JSONParser(); //parser used to parse String to Correct Json format.

JSONObject obj_ComplexData = (JSONObject) parser.parse(genreJson); // Now Your String Converted to a JSONObject Type.

//person tag Array Data is fetched and Stored into a JSONArray Object.
JSONArray obj_arrayPersonData = (JSONArray) parser.parse(obj_ComplexData.get("person").toString());

for (Object person : obj_arrayPersonData ) { //Iterate through all Person Array.
System.out.println(person.get("name"));
System.out.println(person.get("city"));
}

//Select "updated" Tag Json Data.
JSONObject obj_Updated = (JSONObject) parser.parse(obj_ComplexData.get("updated").toString());

System.out.println(obj_Updated.get("time")); //display time tag.
System.out.println(obj_Updated.get("date")); //display date tag.


System.out.println(obj_Updated.get("error")); //display Your Error.

}

关于java - 如何在 Java 中访问 JSON 数据的嵌套元素?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36549315/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com