gpt4 book ai didi

java - 将 JSON 结果转换为 Java 数组

转载 作者:行者123 更新时间:2023-12-01 13:42:27 25 4
gpt4 key购买 nike

我正在尝试将此 json 转换为数组

public class RestWebService {

/**
* @param args
*/
public static void main(String[] args) {
try {
URL url = new URL("http://192.168.18.171/magento/api/rest/products");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
//conn.setRequestMethod("GET");
//conn.setRequestProperty("Accept", "application/json");
System.out.println(conn.getResponseCode());

if(conn.getResponseCode() != 200)
{
throw new RuntimeException("Failed : HTTP error code : "
+ conn.getResponseCode());
}
System.out.println(conn.getInputStream());

BufferedReader br = new BufferedReader(new InputStreamReader(
(conn.getInputStream())));
String output;
System.out.println("Output from Server .... \n");
while ((output = br.readLine()) != null) {
System.out.println(output);


}

conn.disconnect();

}catch (MalformedURLException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

}


}

}

该程序的输出是:

{"21":{"entity_id":"21","type_id":"simple","sku":"HTC Mobiles","description":"HTC is the creator of many award-winning mobile devices and industry firsts. HTC's portfolio includes smartphones and tablets powered by HTC Sense\u2122","short_description":"HTC is the creator of many award-winning mobile devices and industry firsts. HTC's portfolio includes smartphones and tablets powered by HTC Sense\u2122","meta_keyword":"HTC is the creator of many award-winning mobile devices and industry firsts. HTC's portfolio includes smartphones and tablets powered by HTC Sense\u2122","name":"HTC Desire X Dual Sim","meta_title":"SmartPhones | HTC | DESIRE X","meta_description":"HTC is the creator of many award-winning mobile devices and industry firsts. HTC's portfolio includes smartphones and tablets powered by HTC Sense\u2122","regular_price_with_tax":90,"regular_price_without_tax":90,"category_name":"Smartphones","category_id":"20","final_price_with_tax":90,"final_price_without_tax":90,"is_saleable":"1","image_url":"http:\/\/192.168.18.171\/magento\/media\/catalog\/product\/cache\/0\/image\/9df78eab33525d08d6e5fb8d27136e95\/h\/t\/htx-desire-x-ii.jpg"},"19":{"entity_id":"19","type_id":"simple","sku":"Dell laptop","description":"The Inspiron 15R laptop features a 15.6\" screen, color options and up to 3rd Gen Intel\u00ae Core\u2122 processors to keep you stylish and connected.","short_description":"The Inspiron 15R is a well-balanced laptop with something for everyone. Unless you need longer battery life, there's no need to spend more.","meta_keyword":"The Inspiron 15R is a well-balanced laptop with something for everyone. Unless you need longer battery life, there's no need to spend more.","name":"Dell Inspiron 15R","meta_title":"Laptop | Dell | Inspiron15R","meta_description":"The Inspiron 15R is a well-balanced laptop with something for everyone. Unless you need longer battery life, there's no need to spend more.","regular_price_with_tax":120,"regular_price_without_tax":120,"category_name":"Laptop","category_id":"19","final_price_with_tax":115,"final_price_without_tax":115,"is_saleable":"1","image_url":"http:\/\/192.168.18.171\/magento\/media\/catalog\/product\/cache\/0\/image\/9df78eab33525d08d6e5fb8d27136e95\/h\/o\/how-to-deal-with-hp-laptop-power-issues.jpg"}

如何将这个json转换成数组?在 php 中,我们使用 json_decode 来转换为数组。就像这样,我们什么都有。我是新手,请帮助我。

谢谢

最佳答案

native J2SE 无法做到这一点。您需要使用可以执行此操作的 json 框架。以下链接列出了可以执行此操作的框架。

http://www.json.org/

一些常用的框架是

json-lib/jackson/gson

关于java - 将 JSON 结果转换为 Java 数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20609285/

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