Note that there are some explanatory texts on larger screens.

plurals
  1. POParsing json with gson with variant key value to hashmap
    primarykey
    data
    text
    <p>I am parsing a json response using Gson, however i have some key field subject to change like this </p> <pre><code> { "id_item": "5248549", "id_unit": "10865300490", "available_quantity": "8", "title_item": "Casio G-Shock Bluetooth Connected Watch [GB-6900AB-1]", "is_wow_deal": "0", "img": "http://cf4.souqcdn.com/item/52/48/54/9/item_L_20130603124218_5248549.jpg", "additional_attributes": { "Brand": "Casio", "Watch Shape": "Round", "Band Material": "Resin", "Display Type": "Digital", "Targeted Group": "Men", "Type": "Casual Watch" }, "price": "539.00 AED" }, { "id_item": "5124140", "id_unit": "19807600033", "available_quantity": "18", "title_item": "BlackBerry Q10 [English/Black]", "is_wow_deal": "0", "img": "http://cf4.souqcdn.com/item/51/24/14/0/item_L_20130604111349_5124140.jpg", "additional_attributes": { "Brand": "BlackBerry", "Operating System": "BlackBerry OS" }, "price": "2,269.00 AED" }, </code></pre> <p>the <strong>additional_attributes</strong> class is subject to change I am trying to map it to Hashmap so that I can iterate with for each loop;I can go for manual parsing and looping the string response .</p> <p>Below is the java program I am supposing to implement however the approach is wrong , any suggestion is appreciated .</p> <pre><code>public class test { public static void main(String stringagr[]) { String abc = "{ \"additional_attributes\": {\"Brand\": \"Samsung\",\"Operating System\": \"Android\",\"Storage Capacity\": \"8 GB\"}}"; Gson gson = new Gson(); attributes hashMap = gson.fromJson(abc, Attributes.class); for (String string : hashMap.getAdditional_attributes().keySet()) { if (string.length() &gt; 0) { } } } class Attributes { HashMap&lt;String, String&gt; additional_attributes; public HashMap&lt;String, String&gt; getAdditional_attributes() { return additional_attributes; } public void setAdditional_attributes(HashMap&lt;String, String&gt; additional_attributes) { this.additional_attributes = additional_attributes; } }} </code></pre> <p>I have gone through the following approach as well </p> <pre><code>import java.lang.reflect.Type; import java.util.HashMap; import java.util.Map; import com.google.gson.Gson; import com.google.gson.reflect.TypeToken; public class test { public static void main(String stringagr[]) { String abc = "{ \"additional_attributes\": {\"Brand\": \"Samsung\",\"Operating System\": \"Android\",\"Storage Capacity\": \"8 GB\"}}"; Gson gson = new Gson(); Type fooType = new TypeToken&lt;Map&lt;String, String&gt;&gt;() { }.getType(); Map&lt;String, String&gt; map = gson.fromJson(abc, fooType); for (String string : map.keySet()) { if (string.length() &gt; 0) { } } }} </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload