如何使用数字作为字段键在Java中转换JSON对象? [英] How to convert json objects with number as field key in Java?

查看:94
本文介绍了如何使用数字作为字段键在Java中转换JSON对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用的服务器返回一个json对象,其中包含一个对象列表,而不仅仅是一个。

  {
1:{id:1,value:something},
2:{id:2,value: }
}

我想将此json对象转换为对象数组。 p>

我知道我可以使用Gson,并创建一个这样的类:

 公共类数据{
int id;
字符串值;
}

然后使用

  Data data = new Gson()。fromJson(response,Data.class); 

但它仅适用于json对象内的对象。
我不知道如何将数字转换为json对象。



或者我需要将服务器改为回应这样的事情:

  {[id:1,value:something], [id:2,value:some other thing]} 

但是我不想改变服务器,因为我必须更改所有的客户端代码。

解决方案

因为这个json对象使用int作为字段键,在反序列化时无法指定字段键名称。因此,我需要从集合中提取值集合:

  JsonParser parser = new JsonParser(); 
JsonObject obj = parser.parse(json).getAsJsonObject();
Set< Entry< String,JsonElement>> set = obj.entrySet();

现在set包含一组,在我的情况下是< 1,{id:1 ,value:something}>。



因为这里的关键字没用,所以我只需要设置值,所以我迭代集合来提取值集合。 >

  for(Entry< String,JsonElement> j:set){
JsonObject value =(JsonObject)j.getValue();
System.out.println(value.get(id));
System.out.println(value.get(value));



$ b如果你有更复杂的结构,比如嵌套的json对象,你可以拥有一些东西(Entry< String,JsonElement> j:locations){
JsonObject location =(JsonObject){

$ b

  j.getValue(); 
JsonObject coordinate =(JsonObject)location.get(coordinates);
JsonObject address =(JsonObject)location.get(address);
System.out.println(location.get(location_id));
System.out.println(location.get(store_name));
System.out.println(coordinate.get(latitude));
System.out.println(coordinate.get(longitude));
System.out.println(address.get(street_number));
System.out.println(address.get(street_name));
System.out.println(address.get(suburb));
}

希望它有帮助。


The server I am working with returns an json object which contains a list of objects, not just one.

{
"1":{"id":"1","value":"something"},
"2":{"id":"2","value":"some other thing"}
}

I want to convert this json object into an object array.

I know I can use Gson, and create a class like this:

public class Data {
    int id;
    String value;
}

and then use

Data data = new Gson().fromJson(response, Data.class);

But it's only for the objects inside the json object. I don't know how to convert json object with number as keys.

Or alternatively I need to alter the server to response to something like this?:

{["id":"1","value":"something"],["id":"2","value":"some other thing"]}

But I don't want to change to server as I have to change all the client side codes.

解决方案

Because this json object uses int as the field key that you cannot specify the field key name when deserialize it. Thus I need to extract the value set from the set first:

JsonParser parser = new JsonParser();
JsonObject obj = parser.parse(json).getAsJsonObject();
Set<Entry<String,JsonElement>> set = obj.entrySet();

Now "set" contains a set of , in my case is <1,{id:1,value:something}>.

Because the key is useless here, I only need the value set, so I iterate the set to extract the value set.

for (Entry<String,JsonElement> j : set) {
            JsonObject value = (JsonObject) j.getValue();
            System.out.println(value.get("id"));
            System.out.println(value.get("value"));
        }

If you have more complex structure, like nested json objects, you can have something like this:

for (Entry<String,JsonElement> j : locations) {
            JsonObject location = (JsonObject) j.getValue();
            JsonObject coordinate = (JsonObject) location.get("coordinates");
            JsonObject address = (JsonObject) location.get("address");
            System.out.println(location.get("location_id"));
            System.out.println(location.get("store_name"));
            System.out.println(coordinate.get("latitude"));
            System.out.println(coordinate.get("longitude"));
            System.out.println(address.get("street_number"));
            System.out.println(address.get("street_name"));
            System.out.println(address.get("suburb"));
        }

Hope it helps.

这篇关于如何使用数字作为字段键在Java中转换JSON对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆