Sample JSON file ->
--------------------------------------------------------------------------------------------------------------------------
{
"products": [
{
"summary": "Flagship tripod with remote control and pan handle – designed for professionals!",
"averageRating": 4.555555555555555,
"stock": {
"stockLevelStatus": {
"code": "inStock",
"codeLowerCase": "instock"
}
},
"description": "Remote pan-handle with under-hand grip controls zoom in/out, record start/stop, photo mode on/off (camcorder only) Dimensions: maximum height approx. 1,505 mm Dimensions: minimum height approx. 735 mm Weight: approx. 3.2 kg Maximum Load: 5.0 kg Panning Angle: 360 degrees Tilting Angle: 90 degrees down / 70 degrees up Quick-release Mounting Shoe Ball level for quick and easy level adjustment Oil Friction Head ...",
"name": "Flagship tripod with remote control and pan handle",
"availableForPickup": true,
"code": "23355",
"url": "/Open-Catalogue/Cameras/Camera-Accessories-%26-Supplies/Tripods/Flagship-tripod-with-remote-control-and-pan-handle/p/23355",
"priceRange": {},
"price": {
"currencyIso": "USD",
"priceType": "BUY",
"value": 580.38,
"formattedValue": "$580.38"
},
"manufacturer": "Sony",
"volumePricesFlag": false,
"images": [
{
"imageType": "PRIMARY",
"format": "thumbnail",
"url": "/medias/?context=bWFzdGVyfGltYWdlc3wyNDA3fGltYWdlL2pwZWd8aW1hZ2VzL2hkYi9oNzIvODc5NjIzNzEzNTkwMi5qcGd8YTE0YmM0NzE4NzAyZjVlNDcwYWY5NzdjZTA1MzlmMWVkMzYwZWU1ZDA0OGY3MWY1MTdkYzNjMGJhMTk4NTBhMA"
},
{
"imageType": "PRIMARY",
"format": "product",
"url": "/medias/?context=bWFzdGVyfGltYWdlc3wxMTg0NnxpbWFnZS9qcGVnfGltYWdlcy9oNDMvaDY1Lzg3OTYyMTA3OTA0MzAuanBnfDhhNzM5YThjMTYzZmU0OWIyYWQ0Mjg0OWZhYWQ1MmMwM2U2N2RmYzE4NDFmN2RjOWE1YTYzNmQyNmQ3NzNhZDk"
}
]
}
]
}
--------------------------------------------------------------------------------------------------------------------------
JAVA Code->
Here JSON data can be fetched by storing it in a file or directly through the url (exposed via web-service).
--------------------------------------------------------------------------------------------------------------------------
package com.parseProduct;
import java.io.FileReader;
import java.io.IOException;
import java.net.URL;
import java.util.Iterator;
import java.util.Scanner;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
public class ParseFinal {
public static void main(String[] args) throws IOException {
JSONParser parser = new JSONParser();
String s = "http://localhost:9001/rest/v1/electronics/products";
URL url = new URL(s);
Scanner scan = new Scanner(url.openStream());
String str = new String();
while (scan.hasNext())
str += scan.nextLine();
scan.close();
try
{
Object obj = parser.parse(str);
JSONObject jsonObject = (JSONObject)(obj);
JSONArray slideContent = (JSONArray) jsonObject.get("products");
Iterator i = slideContent.iterator();
while (i.hasNext())
{
JSONObject slide = (JSONObject) i.next();
String summary = (String)slide.get("summary");
double averageRating = (double)slide.get("averageRating");
JSONObject stockObj1 = (JSONObject) slide.get("stock");
JSONObject stockoo = (JSONObject) stockObj1.get("stockLevelStatus");
String code = (String)stockoo.get("code");
String codeLowerCase = (String)stockoo.get("codeLowerCase");
String description = (String)slide.get("description");
String name = (String)slide.get("name");
boolean availableForPickup = (boolean)slide.get("availableForPickup");
String productCode = (String)slide.get("code");
String productUrl = (String)slide.get("url");
JSONObject stockObj = (JSONObject) slide.get("price");
String currencyIso = (String)stockObj.get("currencyIso");
String priceType = (String)stockObj.get("priceType");
double value = (double)stockObj.get("value");
String formattedValue = (String)stockObj.get("formattedValue");
String manufacturer = (String)slide.get("manufacturer");
boolean volumePricesFlag = (boolean)slide.get("volumePricesFlag");
JSONArray jsonImage = (JSONArray) slide.get("images");
System.out.println("\n"+"summary:"+summary);
System.out.println("\n"+"averageRating:"+averageRating);
System.out.println("\n"+"stock:"+"stocklevel status:");
System.out.println("\n"+"code:"+code);
System.out.println("\n"+"codeLowerCase: "+codeLowerCase );
System.out.println("\n"+"description:"+description);
System.out.println("\n"+"name:"+name);
System.out.println("\n"+"availableForPickup :"+availableForPickup );
System.out.println("\n"+"productCode :"+productCode);
System.out.println("\n"+"productUrl :"+productUrl);
System.out.println("\n"+"productUrl :"+productUrl);
System.out.println("\n"+"currencyIso:"+currencyIso);
System.out.println("\n"+"priceType:"+priceType);
System.out.println("\n"+"value:"+value);
System.out.println("\n"+"formattedValue:"+formattedValue);
System.out.println("\n"+"manufacturer:"+manufacturer);
System.out.println("\n"+"volumePricesFlag:"+volumePricesFlag);
System.out.println("*************************************");
}
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
--------------------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------------------
{
"products": [
{
"summary": "Flagship tripod with remote control and pan handle – designed for professionals!",
"averageRating": 4.555555555555555,
"stock": {
"stockLevelStatus": {
"code": "inStock",
"codeLowerCase": "instock"
}
},
"description": "Remote pan-handle with under-hand grip controls zoom in/out, record start/stop, photo mode on/off (camcorder only) Dimensions: maximum height approx. 1,505 mm Dimensions: minimum height approx. 735 mm Weight: approx. 3.2 kg Maximum Load: 5.0 kg Panning Angle: 360 degrees Tilting Angle: 90 degrees down / 70 degrees up Quick-release Mounting Shoe Ball level for quick and easy level adjustment Oil Friction Head ...",
"name": "Flagship tripod with remote control and pan handle",
"availableForPickup": true,
"code": "23355",
"url": "/Open-Catalogue/Cameras/Camera-Accessories-%26-Supplies/Tripods/Flagship-tripod-with-remote-control-and-pan-handle/p/23355",
"priceRange": {},
"price": {
"currencyIso": "USD",
"priceType": "BUY",
"value": 580.38,
"formattedValue": "$580.38"
},
"manufacturer": "Sony",
"volumePricesFlag": false,
"images": [
{
"imageType": "PRIMARY",
"format": "thumbnail",
"url": "/medias/?context=bWFzdGVyfGltYWdlc3wyNDA3fGltYWdlL2pwZWd8aW1hZ2VzL2hkYi9oNzIvODc5NjIzNzEzNTkwMi5qcGd8YTE0YmM0NzE4NzAyZjVlNDcwYWY5NzdjZTA1MzlmMWVkMzYwZWU1ZDA0OGY3MWY1MTdkYzNjMGJhMTk4NTBhMA"
},
{
"imageType": "PRIMARY",
"format": "product",
"url": "/medias/?context=bWFzdGVyfGltYWdlc3wxMTg0NnxpbWFnZS9qcGVnfGltYWdlcy9oNDMvaDY1Lzg3OTYyMTA3OTA0MzAuanBnfDhhNzM5YThjMTYzZmU0OWIyYWQ0Mjg0OWZhYWQ1MmMwM2U2N2RmYzE4NDFmN2RjOWE1YTYzNmQyNmQ3NzNhZDk"
}
]
}
]
}
--------------------------------------------------------------------------------------------------------------------------
JAVA Code->
Here JSON data can be fetched by storing it in a file or directly through the url (exposed via web-service).
--------------------------------------------------------------------------------------------------------------------------
package com.parseProduct;
import java.io.FileReader;
import java.io.IOException;
import java.net.URL;
import java.util.Iterator;
import java.util.Scanner;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
public class ParseFinal {
public static void main(String[] args) throws IOException {
JSONParser parser = new JSONParser();
String s = "http://localhost:9001/rest/v1/electronics/products";
URL url = new URL(s);
Scanner scan = new Scanner(url.openStream());
String str = new String();
while (scan.hasNext())
str += scan.nextLine();
scan.close();
try
{
Object obj = parser.parse(str);
JSONObject jsonObject = (JSONObject)(obj);
JSONArray slideContent = (JSONArray) jsonObject.get("products");
Iterator i = slideContent.iterator();
while (i.hasNext())
{
JSONObject slide = (JSONObject) i.next();
String summary = (String)slide.get("summary");
double averageRating = (double)slide.get("averageRating");
JSONObject stockObj1 = (JSONObject) slide.get("stock");
JSONObject stockoo = (JSONObject) stockObj1.get("stockLevelStatus");
String code = (String)stockoo.get("code");
String codeLowerCase = (String)stockoo.get("codeLowerCase");
String description = (String)slide.get("description");
String name = (String)slide.get("name");
boolean availableForPickup = (boolean)slide.get("availableForPickup");
String productCode = (String)slide.get("code");
String productUrl = (String)slide.get("url");
JSONObject stockObj = (JSONObject) slide.get("price");
String currencyIso = (String)stockObj.get("currencyIso");
String priceType = (String)stockObj.get("priceType");
double value = (double)stockObj.get("value");
String formattedValue = (String)stockObj.get("formattedValue");
String manufacturer = (String)slide.get("manufacturer");
boolean volumePricesFlag = (boolean)slide.get("volumePricesFlag");
JSONArray jsonImage = (JSONArray) slide.get("images");
System.out.println("\n"+"summary:"+summary);
System.out.println("\n"+"averageRating:"+averageRating);
System.out.println("\n"+"stock:"+"stocklevel status:");
System.out.println("\n"+"code:"+code);
System.out.println("\n"+"codeLowerCase: "+codeLowerCase );
System.out.println("\n"+"description:"+description);
System.out.println("\n"+"name:"+name);
System.out.println("\n"+"availableForPickup :"+availableForPickup );
System.out.println("\n"+"productCode :"+productCode);
System.out.println("\n"+"productUrl :"+productUrl);
System.out.println("\n"+"productUrl :"+productUrl);
System.out.println("\n"+"currencyIso:"+currencyIso);
System.out.println("\n"+"priceType:"+priceType);
System.out.println("\n"+"value:"+value);
System.out.println("\n"+"formattedValue:"+formattedValue);
System.out.println("\n"+"manufacturer:"+manufacturer);
System.out.println("\n"+"volumePricesFlag:"+volumePricesFlag);
System.out.println("*************************************");
}
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
--------------------------------------------------------------------------------------------------------------------------
JSONObject slide = (JSONObject) i.next();
ReplyDeleteTHIS is exactly what I needed to parse an array within an array in JSON using JSimple. I tried everything, but for some reason not this, and now it clicks. Couldn't find an actual example of parsing a nested array anywhere else on google so THANK YOU
:) Follow my blog to get updates as and when i post new articles.
Delete