如何在Java中转换以数字作为字段键的json对象?

如何在Java中转换以数字作为字段键的json对象?,java,json,gson,Java,Json,Gson,我正在使用的服务器返回一个json对象,其中包含一个对象列表,而不仅仅是一个 { "1":{"id":"1","value":"something"}, "2":{"id":"2","value":"some other thing"} } 我想把这个json对象转换成一个对象数组 我知道我可以使用Gson,并创建如下类: public class Data { int id; String value; } import java.lang.reflect.Type; im

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

{
"1":{"id":"1","value":"something"},
"2":{"id":"2","value":"some other thing"}
}
我想把这个json对象转换成一个对象数组

我知道我可以使用Gson,并创建如下类:

public class Data {
    int id;
    String value;
}
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;
    }
}
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"));
        }
然后使用

Data data = new Gson().fromJson(response, Data.class);
但它只适用于json对象内部的对象。 我不知道如何转换以数字为键的json对象。

或者,我需要更改服务器以响应类似的内容:

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

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

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

public class Data {
    int id;
    String value;
}
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;
    }
}
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"));
        }

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

JsonParser parser = new JsonParser();
JsonObject obj = parser.parse(json).getAsJsonObject();
Set<Entry<String,JsonElement>> set = obj.entrySet();
JsonParser=newjsonparser();
JsonObject obj=parser.parse(json.getAsJsonObject();
Set Set=obj.entrySet();
现在“set”包含一组,在我的例子中是

因为键在这里是无用的,所以我只需要值集,所以我迭代该集以提取值集

for (Entry<String,JsonElement> j : set) {
            JsonObject value = (JsonObject) j.getValue();
            System.out.println(value.get("id"));
            System.out.println(value.get("value"));
        }
for(条目j:set){
JsonObject值=(JsonObject)j.getValue();
System.out.println(value.get(“id”);
System.out.println(value.get(“value”));
}
如果您有更复杂的结构,如嵌套的json对象,您可以有如下内容:

public class Data {
    int id;
    String value;
}
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;
    }
}
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"));
        }
for(条目j:位置){
JsonObject位置=(JsonObject)j.getValue();
JsonObject坐标=(JsonObject)location.get(“坐标”);
JsonObject地址=(JsonObject)location.get(“地址”);
System.out.println(location.get(“location_id”);
System.out.println(location.get(“store_name”);
System.out.println(坐标获取(“纬度”);
System.out.println(坐标获取(“经度”);
System.out.println(address.get(“街道号码”);
System.out.println(address.get(“街道名称”);
System.out.println(address.get(“郊区”);
}

希望有帮助。

这个JSON示例看起来像一个映射。那么,把它转换成一个映射,然后从该映射中获取值()怎么样?它看起来不完全正确,但您的Json字符串不正确,我认为:)这不是“对象列表”。它是一个包含其他对象的对象,这些对象碰巧使用数字作为字段名,或者基本上是一个最基本的
映射。但我找到了一种读取json对象的原始方法,它只提取我需要的字段。无论如何,谢谢你。@SandePanath,我不明白你的问题。接受的答案对你不起作用吗?你试过使用
JsonParser
JsonElement
类吗?我的意思是如何读取第一个值
1
-{这个->“1”:{“id”:“1”,“value”:“something”}在我的示例中,我使用
Map
Map=gson.fromJson(json,类型)。您将在该地图的键集中找到
1
。类似于
map.keySet()
JsonParser类是什么/在哪里?@PranoyC它来自Jackson Java库