0

I'm trying to convert this json into an array

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? In php we use json_decode for convert as array. like that here we have anything. I'm new for this kindly help me.

Thanks,

vinox
  • 401
  • 10
  • 22
  • There is a good tutorial here http://www.mkyong.com/java/how-to-convert-java-object-to-from-json-jackson/ – luanjot Dec 16 '13 at 11:02
  • 2
    possible duplicate :http://stackoverflow.com/questions/2255220/how-to-parse-a-json-and-turn-its-values-into-an-array – Kamlesh Arya Dec 16 '13 at 11:04

2 Answers2

1

Native J2SE can't do this. You need to use a json framework which can do this. Following link lists frameworks that are out there which can do this.

http://www.json.org/

Some of the heavily used frameworks are

json-lib/jackson/gson

Dev Blanked
  • 8,555
  • 3
  • 26
  • 32
0

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

                    }
Nambi
  • 11,944
  • 3
  • 37
  • 49