【问题标题】:Parsing json with gson with variant key value to hashmap使用带有变体键值的 gson 将 json 解析为 hashmap
【发布时间】:2013-06-04 12:27:39
【问题描述】:

我正在使用 Gson 解析 json 响应,但是我有一些关键字段可能会更改 像这样

  {
  "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"
   },

additional_attributes 类可能会发生变化我正在尝试将其映射到 Hashmap,以便我可以对每个循环进行迭代;我可以进行手动解析和循环字符串响应。

下面是我打算实现的java程序,但是方法是错误的,欢迎提出任何建议。

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() > 0) {

        }
    }

}

class Attributes {
    HashMap<String, String> additional_attributes;

    public HashMap<String, String> getAdditional_attributes() {
        return additional_attributes;
    }

    public void setAdditional_attributes(HashMap<String, String> additional_attributes) {
        this.additional_attributes = additional_attributes;
    }
}}

我也经历过以下方法

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<Map<String, String>>() {
    }.getType();
    Map<String, String> map = gson.fromJson(abc, fooType);
    for (String string : map.keySet()) {
        if (string.length() > 0) {

        }
    }

}}

【问题讨论】:

  • attributes,test 类名以小写字母开头,类名以大写字母开头。
  • @vipin sahu 看起来 souq.com 正在那里推出应用程序:D
  • 是的,他们很快就会推出它

标签: java android hashmap gson


【解决方案1】:

您尝试解析的 JSON 字符串 (String abc) 无效...尝试用大括号将其括起来:{ } 表示它是 JSON 对象。

其实在你展示的JSON响应中,你可以看到整个数据都被{ }包围了,所以你可以去掉其他字段,但是你要保持JSON结构,即:

{ "additional_attributes": {...} }

【讨论】:

  • 而不是 [] 我会说,因为它是一个对象列表。
  • @njzk2 不,我不这么认为,JSON 响应是一个带有字段 "additional_attributes" 的对象 ({}),所以如果他删除其他字段,剩下的 JSON 是: { "additional_attributes": {} }
  • 我已经测试了响应并在下面的示例测试中使用 TypeToken 方法进行了编辑和尝试,但仍然无法正常工作
  • @MikO :确实,抱歉,读得快,虽然你一开始指的是大块 json。
【解决方案2】:

我终于解决了,我犯了一些错误,这是答案

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<AdditionalAttributes>() {
    }.getType();
    AdditionalAttributes  map = gson.fromJson(abc, fooType);
    for (String string : map.getAdditional_attributes().keySet()) {
        if (string.length() > 0) {

        }
    }

}
class AdditionalAttributes{
    HashMap<String, String> additional_attributes;

    public HashMap<String, String> getAdditional_attributes() {
        return additional_attributes;
    }

    public void setAdditional_attributes(HashMap<String, String> additional_attributes) {
        this.additional_attributes = additional_attributes;
    }
}}

【讨论】:

    【解决方案3】:

    jSON结构错误,正确的做法是:


    {
     "my_data":[
         {
          "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"
           }
       ]
    }
    

    您应该添加: "my_data":[ 开头和结尾是 ]}

    【讨论】:

    • 可能是结构放错了亲爱的,但我的问题是将数据映射到 HashMap 所以我找到了下面发布的解决方案
    猜你喜欢
    • 1970-01-01
    • 2014-02-12
    • 2013-02-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-26
    相关资源
    最近更新 更多