如何使用java提取json数据

时间:2014-08-11 10:43:17

标签: java json

我无法从java中提取数据。问题是之后将如何获取 name_value_list 。提前谢谢。

{ 
  "id" : "ets7qkt1luugsj828jugs8vuq5",
  "module_name" : "Users",
  "name_value_list" : { "mobile_max_list_entries" : { "name" : "mobile_max_list_entries",
          "value" : null
        },
      "mobile_max_subpanel_entries" : { "name" : "mobile_max_subpanel_entries",
          "value" : null
        },
      "user_currency_id" : { "name" : "user_currency_id",
          "value" : "-99"
        },
      .
      .


      "user_language" : { "name" : "user_language",
          "value" : "en_us"
        },
      "user_name" : { "name" : "user_name",
          "value" : "abcd"
        },
      "user_number_seperator" : { "name" : "user_number_seperator",
          "value" : ","
        }
    }
}

2 个答案:

答案 0 :(得分:0)

我想说尝试使用谷歌的Gson()进行反序列化。

首先,构建实体(或对象映射)类,如:

public class MobileMaxListEntry implements Serializable {
@Expose
@SerializedName("name")
private String name;
@Expose
@SerializedName("name")
private String value;
//getters / setters
}

为您拥有的每个实体执行此操作。然后:

public class ValuesList implements Serializable {
@Expose
@SerializedName("mobile_max_list_entries")
private MobileMaxListEntry mobile_max_list_entries;
@Expose
@SerializedName("mobile_max_subpanel_entries")
private MobileMaxSubpanelEntry mobile_max_subpanel_entries;
//getters/setters for everything
}

然后,响应类:

public class MyResponseClass implements Serializable {  
@Expose
@SerializedName("id")
private String objectId;
@Expose
@SerializedName("module_name")
private String moduleName;
@Expose
@SerializedName("name_value_list")
private ValuesList valuesList;
//getters / setters for everything
}

然后,最后,在您的代码中:

Gson gson = new Gson();
String json = "your_json_here";
MyResponseClass  deserialized = gson.fromJson(json, MyResponseClass.class);

如果出现错误,请尝试为每个类添加默认的空构造函数。

答案 1 :(得分:0)

只需从JSON字符串中提取Map<String,Object>中的数据。

GSON

Type type = new TypeToken<Map<String, Object>>() {}.getType();
Map<String, Object> data = new Gson().fromJson(jsonString, type);
System.out.println(data.get("name_value_list"));

Jackson

ObjectMapper mapper = new ObjectMapper();
TypeReference<Map<String, Object>> typeRef = new TypeReference<Map<String, Object>>() {};
Map<String, Object> data = mapper.readValue(reader, typeRef);
System.out.println(data.get("name_value_list"));

您可以创建一个 POJO 类,它是JSON字符串的精确副本,可以使用GSON在Java对象中进行转换。

示例代码:

class MobileDetail{
    private NameValueList name_value_list;
    private String module_name;
    private String id;
}

class NameValueList{
    private NameValue mobile_max_list_entries;
    private NameValue mobile_max_subpanel_entries;
    private NameValue user_currency_id;
    private NameValue user_language;
    private NameValue user_name;
    private NameValue user_number_seperator;

}

class NameValue{
    private String name;
    private String value;
}

...

MobileDetail data =new Gson().fromJson(reader, MobileDetail.class);
System.out.println(new GsonBuilder().setPrettyPrinting().create().toJson(data));