简体   繁体   English

将JSON结果转换为Java数组

[英]Convert JSON Result to Java Array

I'm trying to convert this json into an array 我正在尝试将此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();

      }


}

} }

the output of this program is: 该程序的输出为:

{"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"}

How to convert this json into an array? 如何将此json转换为数组? In php we use json_decode for convert as array. 在php中,我们使用json_decode将其转换为数组。 like that here we have anything. 这样,我们有任何东西。 I'm new for this kindly help me. 我是新来的,请帮忙。

Thanks, 谢谢,

Native J2SE can't do this. 本机J2SE无法做到这一点。 You need to use a json framework which can do this. 您需要使用可以执行此操作的json框架。 Following link lists frameworks that are out there which can do this. 以下链接列出了可以做到这一点的框架。

http://www.json.org/ http://www.json.org/

Some of the heavily used frameworks are 一些常用的框架是

json-lib/jackson/gson json-lib / jackson / gson

try this 尝试这个

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");

                    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM