[英]How to read JSON Object on the second array with Java/JavaFX code
I am trying to read JSON data with Java, I was successfully to read the first array, below is my JSON report from url. 我正在尝试使用Java读取JSON数据,成功读取了第一个数组,以下是我从url获得的JSON报告。
{
"success":1,
"object":"sale",
"id":"sl987575",
"created":"2019-08-03 21:40:35",
"product_id":"prd00123",
"product_name":"AirBuss",
"amount":"100.00",
"currency":"USD",
"status":"Completed",
"meta":[],
"customer":{
"object":"customer",
"id":"001234",
"email":"someone@email.com",
"name":"Full Name",
"country":null,
"firstname":"Full",
"lastname":"Name"}}
I can read "product_name"
and "status"
but cannot read the "email"
data. 我可以读取
"product_name"
和"status"
但无法读取"email"
数据。
public static void call_me() throws Exception {
String url = "Link WEbsite/api/?apiKey=23459876";
URL obj = new URL(url);
HttpURLConnection con = (HttpURLConnection) obj.openConnection();
// optional default is GET
con.setRequestMethod("GET");
//add request header
con.setRequestProperty("User-Agent", "Mozilla/5.0");
int responseCode = con.getResponseCode();
System.out.println("\nSending 'GET' request to URL : " + url);
System.out.println("Response Code : " + responseCode);
BufferedReader in = new BufferedReader(
new InputStreamReader(con.getInputStream()));
String inputLine;
StringBuffer response = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
//print in String
System.out.println(response.toString());
//Read JSON response and print
JSONObject myResponse = new JSONObject(response.toString());
System.out.println("result after Reading JSON Response");
System.out.println("Product Name : "+myResponse.getString("product_id"));
System.out.println("status : "+myResponse.getString("status"));
System.out.println("Email : "+myResponse.getString("email"));
}
Try this: 尝试这个:
System.out.println("Email : " + myResponse.getJSONObject("customer").getString("email"));
Because 'email', 'country' etc. fields in a nested object named customer . 因为“电子邮件”,“国家”等字段位于名为customer的嵌套对象中。
I just found solution for my own question.. 我刚刚为自己的问题找到了解决方案。
JSONObject customer_data = myResponse.getJSONObject("customer");
System.out.println("Email : "+customer_data.getString("email"));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.