Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在java中使用GSON验证JSON_Java_Json_Gson - Fatal编程技术网

在java中使用GSON验证JSON

在java中使用GSON验证JSON,java,json,gson,Java,Json,Gson,我正在使用GSON验证JSON格式的字符串: String json="{'hashkey':{'calculatedHMAC':'f7b5addd27a221b216068cddb9abf1f06b3c0e1c','secretkey':'12345'},operation':'read','attributes':['name','id'],'otid':'12'}"; Gson gson=new Gson(); Response resp = new Response(); Request

我正在使用GSON验证JSON格式的字符串:

String json="{'hashkey':{'calculatedHMAC':'f7b5addd27a221b216068cddb9abf1f06b3c0e1c','secretkey':'12345'},operation':'read','attributes':['name','id'],'otid':'12'}";
Gson gson=new Gson();
Response resp = new Response();
RequestParameters para = null;
try{
    para = gson.fromJson(json, RequestParameters.class);
}catch(Exception e){
    System.out.println("invalid json format");
}
这很好,但是当我删除下面的引号时,我已经从hashkey中删除了:

"{hashkey':{'calculatedHMAC':'f7b5addd27a221b216068cddb9abf1f06b3c0e1c','secretkey':'12345'},operation':'read','attributes':['name','id'],'otid':'12'}"
它仍然在验证它是否是正确的JSONformat,并且没有抛出任何异常,也没有进入catch body。有什么理由这样做吗?我该如何解决这个问题

RequestParameters类:

public class RequestParameters {
    HashKey hashkey;
    String operation;
    int count;
    int otid;
    String[] attributes;

}

现在,它将第二个引号视为hashkey的一部分。看看下面的json字符串,它是从对象返回的

我在实验室测试过

示例代码:

String json = "{hashkey':{'calculatedHMAC':'f7b5addd27a221b216068cddb9abf1f06b3c0e1c','secretkey':'12345'},operation':'read','attributes':['name','id'],'otid':'12'}";
Gson gson = new Gson();
try {
    Object o = gson.fromJson(json, Object.class);
    System.out.println(new GsonBuilder().setPrettyPrinting().create().toJson(o));
} catch (Exception e) {
    System.out.println("invalid json format");
}
JSON字符串是否需要对键引用


请发布您的RequestParameters类,以便我们可以看到它映射到哪些字段。另请参阅关于gson严格解析解决方法的说明。实际上,在我的例子中,我并没有将其存储在字符串中,但我使用rest客户端将uri发送到rest服务,我发送正文如下:{“hashkey”:{“calculatedHMAC”:“F7B5ADDD227A221B216068CDDB9ABF1F06B3C0E1C”,“secretkey”:“12345”},“operation”:“read”,“attributes”:[“name”,“id”],“otid”:“12”}然后它存储在rest服务中的一个字符串中,rest服务将其作为字符串接收,但当我打印它时。它的打印方式相同:{“hashkey”:{“calculatedHMAC”:“F7B5ADDD27A221B216068CDDB9ABF06B3C0E1C”,“secretkey”:“12345”操作“:“read”,“attributes”:[“name”,“id”],“otid”:“12”}所以这里没有单引号。同样的情况也适用于双引号。
String json = "{hashkey':{'calculatedHMAC':'f7b5addd27a221b216068cddb9abf1f06b3c0e1c','secretkey':'12345'},operation':'read','attributes':['name','id'],'otid':'12'}";
Gson gson = new Gson();
try {
    Object o = gson.fromJson(json, Object.class);
    System.out.println(new GsonBuilder().setPrettyPrinting().create().toJson(o));
} catch (Exception e) {
    System.out.println("invalid json format");
}