-1

以下代码不起作用。无法将 JSON 转换为 JAVA 对象。如何解决?谢谢。

import com.google.gson.Gson;

public class Rest{
    public static void main(String[] args) {
        String json = "{'CBE_GetNewSessionResponse': {'CBE_GetNewSessionResult': '10016-300-0000022151'}}";
        Gson gson = new Gson();
        Data data = gson.fromJson(json, Data.class);
        System.out.println(data.getResponse());
    }
}

class Data {
    private CBE_GetNewSessionResponse response;

    public CBE_GetNewSessionResponse getResponse() {
        return response;
    }

    public void setResponse(CBE_GetNewSessionResponse response) {
        this.response = response;
    }

    class CBE_GetNewSessionResponse{

        private String  CBE_GetNewSessionResult;

        public String getCBE_GetNewSessionResult() {
            return CBE_GetNewSessionResult;
        }

        public void setCBE_GetNewSessionResult(String cBE_GetNewSessionResult) {
            CBE_GetNewSessionResult = cBE_GetNewSessionResult;
        }


    }
}
4

2 回答 2

1

文档Gson状态

使用字段与 getter 来指示 Json 元素

一些 Json 库使用类型的 getter 来推断 Json 元素。我们选择使用非瞬态、静态或合成的所有字段(在继承层次结构上)。我们这样做是因为并非所有类都是用适当命名的 getter 编写的。此外,getXXX 或 isXXX 可能是语义而不是指示属性。

换句话说,您的字段需要按照它出现在 JSON 中的方式命名,反之亦然。

private CBE_GetNewSessionResponse CBE_GetNewSessionResponse;

然后它将正确地将您的 JSON 映射到您的对象结构。

于 2013-10-01T17:38:17.257 回答
0

您在 Data 中命名了字段,response而它CBE_GetNewSessionResponse在 JSON 中命名。试试这种方式

public class Rest{
    public static void main(String[] args) {
        String json = "{'CBE_GetNewSessionResponse': {" +
                "'CBE_GetNewSessionResult': '10016-300-0000022151'}" +
                "}";
        Gson gson = new Gson();
        Data data = gson.fromJson(json, Data.class);
        System.out.println(data.getCBE_GetNewSessionResponse().getCBE_GetNewSessionResult());
    }
}

class Data {
    private CBE_GetNewSessionResponse CBE_GetNewSessionResponse;

    public CBE_GetNewSessionResponse getResponse() {
        return CBE_GetNewSessionResponse;
    }

    public void setResponse(CBE_GetNewSessionResponse response) {
        this.CBE_GetNewSessionResponse = response;
    }

    class CBE_GetNewSessionResponse{

        private String  CBE_GetNewSessionResult;

        public String getCBE_GetNewSessionResult() {
            return CBE_GetNewSessionResult;
        }

        public void setCBE_GetNewSessionResult(String cBE_GetNewSessionResult) {
            CBE_GetNewSessionResult = cBE_GetNewSessionResult;
        }
    }
}

Also in case you want to have different names in Data class fields and JSon keys you would need to implement your own FieldNamingStrategy like

class MyNameStrategy implements FieldNamingStrategy {

    @Override
    public String translateName(Field f) {
        if (f.getName().equals("response")) {
            return "CBE_GetNewSessionResponse";
        } else {
            return f.getName();
        }
    }
}

and use it while creating Gson object

Gson gson = new GsonBuilder().setFieldNamingStrategy(new MyNameStrategy()).create();
于 2013-10-01T17:39:34.883 回答