通过Gson建立一个json

时间:2014-07-17 10:38:00

标签: java json oop gson

我这里有一个小项目,从表中获取数据并使用json将其作为json返回,但它返回的json似乎是一个字符串。它有反斜杠,有人知道它会导致什么。

Result result = new Result();
        result.responseCode = "00";
        result.responseMessage = "Successful";

        GsonBuilder builder = new GsonBuilder();
        Gson gson = builder.serializeNulls().create();

        String x = "x";
        String name="",address ="",msisdn="",email="";
        Details details = new Details();
        for(DataRow dr : drw_){

            name = dr.get("NAME").toString();
            details.name = name;

            address = dr.get("ADDRESS").toString();
            details.address = address;

            msisdn = dr.get("CONTACTNUMBER").toString();
            details.msisdn = msisdn;

            email = dr.get("EMAIL").toString();
            details.email = email;

            gson.toJson(details);

            result.detailsList.add(gson.toJson(details));

        }
        System.out.println(gson.toJson(details));
        System.out.println(gson.toJson(result));

示例输出:

{"responseMessage":"Successful",
 "responseCode":"00",
  "detailsList":["{\"name\":\"name1\",
               \"address\":\"address st 1\",
               \"msisdn\":\"09211231234\",
               \"email\":\"email@someweb.com\"}"
            ,"{
               \"name\":\"testname\",
               \"address\":\"testadress st 1 CITY\",
               \"msisdn\":\"+639171234567\",
               \"email\":\"myemail@someweb.com\"}
            "]
}

预期产出:

{"responseMessage":"Successful",
"responseCode":"00",
"detailsList":[{"name":"name1",
               "address":"address st 1",
               "msisdn":"09211231234",
               "email":"email@someweb.com"}
            ,{
               "name":"testname",
               "address":"testadress st 1 CITY",
               "msisdn":"+639171234567",
               "email":"myemail@someweb.com"}
 ]}

4 个答案:

答案 0 :(得分:0)

您正在构建一个json,其中detailList list字段的每个元素都是一个字符串,其中包含以前生成的其他Json的字符串表示形式:

result.detailsList.add(gson.toJson(details));

您的detailsList似乎是一个字符串列表。 detailsList应该是类Details对象的集合,而不是Strings

这样,上面的行应改为:

result.detailsList.add(details);

否则,现在正在发生这种情况,您正在编码包含引号的字符串,因此这些引号正在使用转义字符进行编码。

答案 1 :(得分:0)

斜线是因为"值。这可以打破一个字符串。例如

String x = "{"name":"name1"}" // Not a valid string

String x = "{\"name\":\"name1\"}" // Valid string

尝试一下,编译器会显示错误。

答案 2 :(得分:0)

你将它转换为gson两次..所以第二次解析会转义双引号。试试这个

    Result result = new Result();
    result.responseCode = "00";
    result.responseMessage = "Successful";

    GsonBuilder builder = new GsonBuilder();
    Gson gson = builder.serializeNulls().create();

    String x = "x";
    String name="",address ="",msisdn="",email="";
    Details details = new Details();
    for(DataRow dr : drw_){

        name = dr.get("NAME").toString();
        details.name = name;

        address = dr.get("ADDRESS").toString();
        details.address = address;

        msisdn = dr.get("CONTACTNUMBER").toString();
        details.msisdn = msisdn;

        email = dr.get("EMAIL").toString();
        details.email = email;

        result.detailsList.add(details);

    }
    System.out.println(gson.toJson(details));
    System.out.println(gson.toJson(result));

答案 3 :(得分:0)

您尝试过:result.detailsList.add(details);?您应该让JSON一次性序列化所有内容,而不是将JSON字符串放在另一个对象中。