0

I have a custom portlet with one dropdown item. Additionally i have a products.json file.

My Requirement

The dropdown shows the products and when the user clicks on a product, the corresponding details are fetched from the json file and displayed.

How do i do that ?

{
"products" : [  
  {  
     "id":"prod_1001",
     "name":" iPhone 6",
     "type":"Mobile",
     "asset_tag":"<asset_tag>",
     "serial_no":"34238941",
     "cost_center":"",
     "cpu_count":"2",
     "cpu_socket":"8",
     "cpu_name":"Intel",
     "cpu_type":"core",
     "cpu_speed_mhz":"2200",
     "disk_gb":"64",
     "ram_mb":"2048",
     "first_discoverd_date":"",
     "recent_discovery_date":"",
     "service_start_date":"",
     "warrent_expiry_date":"",
     "ip_address":"10.10.10.1",
     "lease_contract":"",
     "lease_start_date":"",
     "lease_end_date":"",
     "location":"India",
     "sku":"IPHONE-6",
     "cost":"500",
     "currency":"$",
     "cpu":"",
     "power_supply":"12v",
     "hard_drive":"1",
     "memory":[  
        {  
           "memory_type":"ram",
           "sku":"RAM-4GB"
        },
        {  
           "memory_type":"Internal",
           "sku":"INTERNAL-128GB"
        }
     ],
     "warranty_expiration":"",
     "gps_location":"India",
     "network_domain":"domain",
     "network_segment":"segment",
     "group":"Product",
     "company":"Apple"
  },
  {  
     "id":"prod_1005",
     "name":"Nokia",
     "type":"Mobile",
     "asset_tag":"<asset_tag>",
     "serial_no":"34238941",
     "cost_center":"",
     "cpu_count":"2",
     "cpu_socket":"8",
     "cpu_name":"Intel",
     "cpu_type":"core",
     "cpu_speed_mhz":"2200",
     "disk_gb":"32",
     "ram_mb":"2048",
     "first_discoverd_date":"",
     "recent_discovery_date":"",
     "service_start_date":"",
     "warrent_expiry_date":"",
     "ip_address":"10.10.10.1",
     "lease_contract":"",
     "lease_start_date":"",
     "lease_end_date":"",
     "location":"India",
     "sku":"IPHONE-6",
     "cost":"500",
     "currency":"$",
     "cpu":"",
     "power_supply":"12v",
     "hard_drive":"1",
     "memory":[  
        {  
           "memory_type":"ram",
           "sku":"RAM-4GB"
        },
        {  
           "memory_type":"Internal",
           "sku":"INTERNAL-128GB"
        }
     ],
     "warranty_expiration":"",
     "gps_location":"India",
     "network_domain":"domain",
     "network_segment":"segment",
     "group":"Product",
     "company":"Microsoft"
  },
cafebabe1991
  • 4,928
  • 2
  • 34
  • 42

2 Answers2

0

You can use a number of JSON libraries like the json maven library available here

You can use them to convert JSON strings to JSONObjects and use accessor methods defined in those objects to parse through the JSON Data

0

Also try Gson libray

You have 2 choices:

  • create a Product javabean by yourself and get result as a list
  • get result list as a generic map

List<Product> list = gson.fromJson(json, new TypeToken<List<Product>)(){}.getType());

or

Map<String, Object> map = gson.fromJson(json, new TypeToken<Map<String, Object>>(){}.getType());
Fabrizio Stellato
  • 1,727
  • 21
  • 52