简体   繁体   English

JSON到Java HashMap对象

[英]JSON to Java HashMap Object

From an api call I am getting this type of data: 通过一个api调用,我得到了这种类型的数据:

"value": {
      "technify_id": "1000051500309",
      "product": "{technify_id:'1000051500309',description:'Light weight 100% cotton fabric',id_uuid:'-100005-3008',image:'http://www.wearego.com/image/catalog/acc/lower/LK0060-BK0.jpg',is_configurable:0,manufacturer_id:'1000050',meta_data:{'Yousuf':'Iqbal','bilal':'ahmed'},seller:{id:'1000031127',name:'BNG Distributer'},manufacturer:{id:'100003521',name:'Ego'},meta_description:'',meta_keyword:'',meta_title:'Spring Collection 2016',model:'LK0060-BK0',name:'ELEPHANT PARADE',price:1200.0,product_attributes:[{attribute_id:'10000516',attribute_name:'1 x ELEPHANT PARADE',text:'1 x ELEPHANT PARADE'}],product_categories:['90'],product_url:'asdasd',quantity:10,requires_shipping:0,sku:'LK0060-BK0',sort_order:'1',status:1,store_date_added:'2016-11-24 23:03:57',store_date_available:'2016-03-22',store_date_modified:'2017-11-14 08:15:00',store_id:'100005',store_name:'WearEgo',store_product_id:'3008',tag:'',tax_id:'1000050',updated_at:'14/12/2007',variants:[{id:'1000051500309_5',technify_id:'1000051500309',price:1200.0,sku:'LK0060-BK0',height:'0.2',created_at:'2017-09-22 03:44:40',updated_at:'2017-09-22 03:44:40',quantity:22,weight:0.2,weight_unit:'kg',image:'http://prod.technifydev.com/vendor/ego/image/catalog/festive/soulful-01 (2).JPG',additional_images:[{image:'http://www.companion.com.pk/image/catalog/festive/soulful-01 (3).JPG',id:'10000523651',variation_id:'1000051500309_5'},{image:'http://www.companion.com.pk/image/catalog/festive/soulful-01 (1).JPG',id:'10000523652',variation_id:'1000051500309_5'}],product_specials:[],option_values:[{option_id:'1',option_name:'null',option_value_id:'5',option_value_name:NULL}],height_unit:NULL},{id:'1000051500309_4',technify_id:'1000051500309',price:1200.0,sku:'LK0060-BK0',height:'0.2',created_at:'2017-09-22 03:44:40',updated_at:'2017-09-22 03:44:40',quantity:22,weight:0.2,weight_unit:'kg',image:'http://prod.technifydev.com/vendor/ego/image/catalog/festive/soulful-01 (2).JPG',additional_images:[{image:'http://www.companion.com.pk/image/catalog/festive/soulful-01 (3).JPG',id:'10000523651',variation_id:'1000051500309_4'},{image:'http://www.companion.com.pk/image/catalog/festive/soulful-01 (1).JPG',id:'10000523652',variation_id:'1000051500309_4'}],product_specials:[],option_values:[{option_id:'1',option_name:'null',option_value_id:'4',option_value_name:'null'}],height_unit:NULL},{id:'1000051500309_3',technify_id:'1000051500309',price:22.0,sku:'LK0060-BK0',height:'0.2',created_at:'2017-09-22 03:44:40',updated_at:'2017-09-22 03:44:40',quantity:22,weight:0.2,weight_unit:'kg',image:'http://prod.technifydev.com/vendor/ego/image/catalog/festive/soulful-01 (2).JPG',additional_images:[{image:'http://www.companion.com.pk/image/catalog/festive/soulful-01 (3).JPG',id:'10000523651',variation_id:'1000051500309_3'},{image:'http://www.companion.com.pk/image/catalog/festive/soulful-01 (1).JPG',id:'10000523652',variation_id:'1000051500309_3'}],product_specials:[{date_start:'0000-00-00',price:605.79,product_special_id:'1000055564',date_end:'0000-00-00'}],option_values:[{option_id:'1',option_name:'null',option_value_id:'3',option_value_name:'null'}],height_unit:NULL},{id:'1000051500309_2',technify_id:'1000051500309',price:1200.0,sku:'LK0060-BK0',height:'0.2',created_at:'2017-09-22 03:44:40',updated_at:'2017-09-22 03:44:40',quantity:12,weight:0.2,weight_unit:'kg',image:'http://prod.technifydev.com/vendor/ego/image/catalog/festive/soulful-01 (2).JPG',additional_images:[{image:'http://www.companion.com.pk/image/catalog/festive/soulful-01 (3).JPG',id:'10000523651',variation_id:'1000051500309_2'},{image:'http://www.companion.com.pk/image/catalog/festive/soulful-01 (1).JPG',id:'10000523652',variation_id:'1000051500309_2'}],product_specials:[{date_start:'0000-00-00',price:605.79,product_special_id:'1000055564',date_end:'0000-00-00'}],option_values:[{option_id:'1',option_name:'null',option_value_id:'2',option_value_name:'null'}],height_unit:NULL},{id:'1000051500309_1',technify_id:'1000051500309',price:1200.0,sku:'LK0060-BK0',height:'0.2',created_at:'2017-09-22 03:44:40',updated_at:'2017-09-22 03:44:40',quantity:11,weight:0.2,weight_unit:'kg',image:'http://prod.technifydev.com/vendor/ego/image/catalog/festive/soulful-01 (2).JPG',additional_images:[{image:'http://www.companion.com.pk/image/catalog/festive/soulful-01 (3).JPG',id:'10000523651',variation_id:'1000051500309_1'},{image:'http://www.companion.com.pk/image/catalog/festive/soulful-01 (1).JPG',id:'10000523652',variation_id:'1000051500309_1'}],product_specials:[{date_start:'0000-00-00',price:605.79,product_special_id:'1000055564',date_end:'0000-00-00'}],option_values:[{option_id:'1',option_name:'null',option_value_id:'1',option_value_name:'null'}],height_unit:NULL}],view_count:'1',weight:0.2}",
      "store_id": "100005",
      "updated_at": 1506033885000
    }

as it is clear that in product field data is of type string but we can also see that it is in json format but keys have no quotations and values are in single quote due to which I am not able to convert it from GSON. 因为很明显,在产品字段中,数据是字符串类型的,但我们也可以看到它是json格式的,但是键没有引号,并且值都在单引号中,因此我无法将其从GSON转换。 Is there any way to handle this scenario and extract proper JSON from this type of data? 有什么方法可以处理这种情况并从这种类型的数据中提取适当的JSON?

Have you tried this? 你有尝试过吗?

    String rawdata=" {\n" +
            "      \"technify_id\": \"1000051500309\",\n" +
            "      \"product\": \"{technify_id:'1000051500309',description:'Light weight 100% cotton fabric',id_uuid:'-100005-3008',image:'http://www.wearego.com/image/catalog/acc/lower/LK0060-BK0.jpg',is_configurable:0,manufacturer_id:'1000050',meta_data:{'Yousuf':'Iqbal','bilal':'ahmed'},seller:{id:'1000031127',name:'BNG Distributer'},manufacturer:{id:'100003521',name:'Ego'},meta_description:'',meta_keyword:'',meta_title:'Spring Collection 2016',model:'LK0060-BK0',name:'ELEPHANT PARADE',price:1200.0,product_attributes:[{attribute_id:'10000516',attribute_name:'1 x ELEPHANT PARADE',text:'1 x ELEPHANT PARADE'}],product_categories:['90'],product_url:'asdasd',quantity:10,requires_shipping:0,sku:'LK0060-BK0',sort_order:'1',status:1,store_date_added:'2016-11-24 23:03:57',store_date_available:'2016-03-22',store_date_modified:'2017-11-14 08:15:00',store_id:'100005',store_name:'WearEgo',store_product_id:'3008',tag:'',tax_id:'1000050',updated_at:'14/12/2007',variants:[{id:'1000051500309_5',technify_id:'1000051500309',price:1200.0,sku:'LK0060-BK0',height:'0.2',created_at:'2017-09-22 03:44:40',updated_at:'2017-09-22 03:44:40',quantity:22,weight:0.2,weight_unit:'kg',image:'http://prod.technifydev.com/vendor/ego/image/catalog/festive/soulful-01 (2).JPG',additional_images:[{image:'http://www.companion.com.pk/image/catalog/festive/soulful-01 (3).JPG',id:'10000523651',variation_id:'1000051500309_5'},{image:'http://www.companion.com.pk/image/catalog/festive/soulful-01 (1).JPG',id:'10000523652',variation_id:'1000051500309_5'}],product_specials:[],option_values:[{option_id:'1',option_name:'null',option_value_id:'5',option_value_name:NULL}],height_unit:NULL},{id:'1000051500309_4',technify_id:'1000051500309',price:1200.0,sku:'LK0060-BK0',height:'0.2',created_at:'2017-09-22 03:44:40',updated_at:'2017-09-22 03:44:40',quantity:22,weight:0.2,weight_unit:'kg',image:'http://prod.technifydev.com/vendor/ego/image/catalog/festive/soulful-01 (2).JPG',additional_images:[{image:'http://www.companion.com.pk/image/catalog/festive/soulful-01 (3).JPG',id:'10000523651',variation_id:'1000051500309_4'},{image:'http://www.companion.com.pk/image/catalog/festive/soulful-01 (1).JPG',id:'10000523652',variation_id:'1000051500309_4'}],product_specials:[],option_values:[{option_id:'1',option_name:'null',option_value_id:'4',option_value_name:'null'}],height_unit:NULL},{id:'1000051500309_3',technify_id:'1000051500309',price:22.0,sku:'LK0060-BK0',height:'0.2',created_at:'2017-09-22 03:44:40',updated_at:'2017-09-22 03:44:40',quantity:22,weight:0.2,weight_unit:'kg',image:'http://prod.technifydev.com/vendor/ego/image/catalog/festive/soulful-01 (2).JPG',additional_images:[{image:'http://www.companion.com.pk/image/catalog/festive/soulful-01 (3).JPG',id:'10000523651',variation_id:'1000051500309_3'},{image:'http://www.companion.com.pk/image/catalog/festive/soulful-01 (1).JPG',id:'10000523652',variation_id:'1000051500309_3'}],product_specials:[{date_start:'0000-00-00',price:605.79,product_special_id:'1000055564',date_end:'0000-00-00'}],option_values:[{option_id:'1',option_name:'null',option_value_id:'3',option_value_name:'null'}],height_unit:NULL},{id:'1000051500309_2',technify_id:'1000051500309',price:1200.0,sku:'LK0060-BK0',height:'0.2',created_at:'2017-09-22 03:44:40',updated_at:'2017-09-22 03:44:40',quantity:12,weight:0.2,weight_unit:'kg',image:'http://prod.technifydev.com/vendor/ego/image/catalog/festive/soulful-01 (2).JPG',additional_images:[{image:'http://www.companion.com.pk/image/catalog/festive/soulful-01 (3).JPG',id:'10000523651',variation_id:'1000051500309_2'},{image:'http://www.companion.com.pk/image/catalog/festive/soulful-01 (1).JPG',id:'10000523652',variation_id:'1000051500309_2'}],product_specials:[{date_start:'0000-00-00',price:605.79,product_special_id:'1000055564',date_end:'0000-00-00'}],option_values:[{option_id:'1',option_name:'null',option_value_id:'2',option_value_name:'null'}],height_unit:NULL},{id:'1000051500309_1',technify_id:'1000051500309',price:1200.0,sku:'LK0060-BK0',height:'0.2',created_at:'2017-09-22 03:44:40',updated_at:'2017-09-22 03:44:40',quantity:11,weight:0.2,weight_unit:'kg',image:'http://prod.technifydev.com/vendor/ego/image/catalog/festive/soulful-01 (2).JPG',additional_images:[{image:'http://www.companion.com.pk/image/catalog/festive/soulful-01 (3).JPG',id:'10000523651',variation_id:'1000051500309_1'},{image:'http://www.companion.com.pk/image/catalog/festive/soulful-01 (1).JPG',id:'10000523652',variation_id:'1000051500309_1'}],product_specials:[{date_start:'0000-00-00',price:605.79,product_special_id:'1000055564',date_end:'0000-00-00'}],option_values:[{option_id:'1',option_name:'null',option_value_id:'1',option_value_name:'null'}],height_unit:NULL}],view_count:'1',weight:0.2}\",\n" +
            "      \"store_id\": \"100005\",\n" +
            "      \"updated_at\": 1506033880000\n" +
            "    }";

    HashMap<String,Object> stringObjectHashMap = new
            Gson().fromJson(rawdata,new TypeToken<HashMap<String,Object>>(){}.getType());


    //System.out.println(stringObjectHashMap);

    JSONObject jsonObject=new JSONObject(stringObjectHashMap.get("product").toString());
    System.out.println(jsonObject);

It is working for me. 它为我工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM