我正在尝试将此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进行转换为数组。 像这样我们有什么。 我是新来的,请帮助我。
谢谢,
答案 0 :(得分:1)
原生J2SE不能这样做。您需要使用可以执行此操作的json框架。以下链接列出了可以执行此操作的框架。
一些使用频繁的框架是
JSON-LIB /杰克逊/ GSON
答案 1 :(得分:0)
试试这个
JSONObject jObject= new JSONObject(output);
JSONArray menuObject = new JSONArray(jObject.getString("21"));
String sku,entity_id;
for (int i = 0; i<menuObject.length(); i++)
{
entity_id=menuObject.getJSONObject(i).getString("entity_id");
sku= menuObject.getJSONObject(i).getString("entity_id");
}