7

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

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

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

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

public class Data {
    int id;
    String value;
}

然后使用

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

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

或者我需要更改服务器以响应这样的事情?:

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

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

4

2 回答 2

1

你的 JSON 看起来很奇怪。如果无法更改,则必须将其反序列化为Map. 示例源代码可能如下所示:

import java.lang.reflect.Type;
import java.util.HashMap;
import java.util.Map;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;

public class GsonProgram {

    public static void main(String... args) throws Exception {
        Gson gson = new GsonBuilder().create();
        String json = "{\"1\":{\"id\":\"1\",\"value\":\"something\"},\"2\":{\"id\":\"2\",\"value\":\"some other thing\"}}";

        Type type = new TypeToken<HashMap<String, HashMap<String, String>>>() {}.getType();
        Map<String, Map<String, String>> map = gson.fromJson(json, type);
        for (Map<String, String> data : map.values()) {
            System.out.println(Data.fromMap(data));
        }
    }
}

class Data {

    private int id;
    private String value;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getValue() {
        return value;
    }

    public void setValue(String value) {
        this.value = value;
    }

    @Override
    public String toString() {
        return "Data [id=" + id + ", value=" + value + "]";
    }

    public static Data fromMap(Map<String, String> properties) {
        Data data = new Data();
        data.setId(new Integer(properties.get("id")));
        data.setValue(properties.get("value"));

        return data;
    }
}

上面的程序打印:

Data [id=2, value=some other thing]
Data [id=1, value=something]
于 2013-07-24T08:16:03.903 回答
1

因为这个 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}>。

因为这里key没用,我只需要value集合,所以我迭代set提取value集合。

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

如果你有更复杂的结构,比如嵌套的 json 对象,你可以有这样的东西:

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"));
        }

希望能帮助到你。

于 2013-07-31T02:36:56.710 回答