1

I need to parse json files using java to use in jsp files.

For example, using the REST API in Magento whene I call this URL : magento/api/rest/products?limit=2.

I have something like this :

{
"16":
{"entity_id":"16",
"attribute_set_id":"38",
"type_id":"simple",
"sku":"n2610",
"model":"2610",
"dimension":"4.1 x 1.7 x 0.7 inches ",
"description":"The Nokia 2610 is",
"in_depth":"Integrated camera",
"activation_information":"Conditional 250ドル Equipment Discount Included",
"meta_keyword":"Nokia 2610, cell, phone, ",
"short_description":"The words \"entry level\" no longer mean \"low-end,\" especially when it comes to the Nokia 2610",
"custom_layout_update":"",
"status":"1",
"tax_class_id":"2",
"color":"24",
"visibility":"4",
"weight":"3.2000",
"price":"149.9900",
"cost":"20.0000",
"name":"Nokia 2610 Phone",
"manufacturer":"20",
"url_key":"nokia-2610-phone",
"meta_title":"Nokia 2610",
"meta_description":"Offering advanced media and calling features without breaking the bank, The Nokia 2610 is an easy to use",
"gift_message_available":"",
"options_container":"container2",
"custom_design":""},
"17":
{"entity_id":"17",
"attribute_set_id":"38",
"type_id":"simple",
"sku":"bb8100",
"model":"8100",
"dimension":"4.2 x 2 x 0.6 inches ",
"description":" Like the BlackBerry 7105t",
"in_depth":"1.3 mega pixel camera to capture those special moments",
"activation_information":"CONDITIONAL 250ドル Equipment Discount Included",
"meta_keyword":"Blackberry, 8100, pearl, cell, phone",
"short_description":"The BlackBerry 8100 Pearl is a departure from the form factor of previous BlackBerry devices",
"custom_layout_update":"",
"color":"23",
"status":"1",
"tax_class_id":"2",
"visibility":"4",
"weight":"15.2000",
"price":"349.9900",
"cost":"29.9900",
"name":"BlackBerry 8100 Pearl",
"manufacturer":"21",
"url_key":"blackberry-8100-pearl",
"meta_title":"BlackBerry 8100 Pearl",
"meta_description":"BlackBerry 8100 Pearl sports a large 240 x 260 screen",
"gift_message_available":"",
"options_container":"container2",
"custom_design":""}}

I need to extract some informations : name, price, description ... but I don't know how to do it.

I read some tutorials http://www.journaldev.com/2321/google-gson-api-for-json-processing-example-tutorial

The problem is that Magento Json files are very different and special comparing to this one for example:

{
 "empID": 100,
 "name": "David",
 "permanent": false,
 "address": {
 "street": "BTM 1st Stage",
 "city": "Bangalore",
 "zipcode": 560100
 },
 "phoneNumbers": [
 123456,
 987654
 ],
 "role": "Manager",
 "cities": [
 "Los Angeles",
 "New York"
 ],
 "properties": {
 "age": "28 years",
 "salary": "1000 Rs"
 }
}

Because we have "16"and "17"which are "dynamic".

VLAZ
29.6k9 gold badges65 silver badges88 bronze badges
asked Apr 23, 2014 at 8:27
3
  • Your first json is invalid.. missing comma after "description":" Like the BlackBerry 7105t" Commented Apr 23, 2014 at 8:29
  • Did it solve your problem ? Commented Apr 23, 2014 at 9:02
  • No it didn't solve it. Commented Apr 23, 2014 at 9:07

2 Answers 2

1

first json is invalid..missing comma after "description":"Like the BlackBerry 7105t"


this type of json is a map,so 16 and 17 is the key in the map.

then here you can use the code to decode:

BTW:What I read from file is your json String

public static void main(String[] args) throws FileNotFoundException {
 Gson gson = new Gson();
 String json = readFromFile(new File("json.txt"));
 HashMap<String, GObj> maps = gson.fromJson(json, new TypeToken<HashMap<String, GObj>>(){}.getType());
 System.out.println(maps.get("16").description);
}
static String readFromFile(File file) throws FileNotFoundException {
 Scanner scanner = new Scanner(new FileInputStream(file));
 StringBuilder builder = new StringBuilder();
 while (scanner.hasNext()) {
 builder.append(scanner.nextLine());
 }
 scanner.close();
 return builder.toString();
}
static class GObj {
 public int entity_id;
 public int attribute_set_id;
 public int status;
 public int tax_class_id;
 public int color;
 public int visibility;
 public int manufacturer;
 public double weight;
 public double price;
 public double cost;
 public String type_id;
 public String sku;
 public String model;
 public String dimension;
 public String description;
 public String in_depth;
 public String activation_information;
 public String meta_keyword;
 public String short_description;
 public String custom_layout_update;
 public String name;
 public String url_key;
 public String meta_title;
 public String meta_description;
 public String gift_message_available;
 public String options_container;
 public String custom_design;
}
answered Apr 23, 2014 at 9:08
Sign up to request clarification or add additional context in comments.

1 Comment

I used this to access dynamically: Iterator it = maps.keySet().iterator(); while (it.hasNext()) { System.out.println(maps.get(it.next()).price); }
0

Thanks Mike. Finally, here's my java file:

package main;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Scanner;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
public class Test {
 public static void main(String[] args) throws FileNotFoundException {
 Gson gson = new Gson();
 String json = readFromFile(new File("test.txt"));
 HashMap<String, GObj> maps = gson.fromJson(json, new TypeToken<HashMap<String, GObj>>(){}.getType());
 System.out.println(maps.size());
 System.out.println(maps.keySet());
 Iterator it = maps.keySet().iterator();
 while (it.hasNext()) {
 System.out.println(maps.get(it.next()).price);
 }
 }
 static String readFromFile(File file) throws FileNotFoundException {
 Scanner scanner = new Scanner(new FileInputStream(file));
 StringBuilder builder = new StringBuilder();
 while (scanner.hasNext()) {
 builder.append(scanner.nextLine());
 }
 scanner.close();
 return builder.toString();
 }
 static class GObj {
 public String entity_id;
 public String attribute_set_id;
 public String status;
 public String tax_class_id;
 public String color;
 public String visibility;
 public String manufacturer;
 public String weight;
 public String price;
 public String cost;
 public String type_id;
 public String sku;
 public String model;
 public String dimension;
 public String description;
 public String in_depth;
 public String activation_information;
 public String meta_keyword;
 public String short_description;
 public String custom_layout_update;
 public String name;
 public String url_key;
 public String meta_title;
 public String meta_description;
 public String gift_message_available;
 public String options_container;
 public String custom_design;
 }
}

And the output is :

2
[17, 16]
349.9900
149.9900
answered Apr 24, 2014 at 10:53

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.