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,
-
There is a good tutorial here mkyong.com/java/how-to-convert-java-object-to-from-json-jacksonluanjot– luanjot2013年12月16日 11:02:20 +00:00Commented Dec 16, 2013 at 11:02
-
2possible duplicate :stackoverflow.com/questions/2255220/…Kamlesh Meghwal– Kamlesh Meghwal2013年12月16日 11:04:57 +00:00Commented Dec 16, 2013 at 11:04
2 Answers 2
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.
Some of the heavily used frameworks are
json-lib/jackson/gson
answered Dec 16, 2013 at 11:07
Dev Blanked
8,9153 gold badges28 silver badges33 bronze badges
Sign up to request clarification or add additional context in comments.
Comments
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");
}
answered Dec 16, 2013 at 11:28
Nambi
12k4 gold badges40 silver badges50 bronze badges
1 Comment
vinox
i cannot add the output in jsonobject
lang-java