Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/357.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/230.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 如何在没有名称和动态的情况下反序列化json对象?_Java_Android_Json_Gson_Pojo - Fatal编程技术网

Java 如何在没有名称和动态的情况下反序列化json对象?

Java 如何在没有名称和动态的情况下反序列化json对象?,java,android,json,gson,pojo,Java,Android,Json,Gson,Pojo,我有一个JSON: { "username": [ "A user with that username already exists." ], "ruc": [ "user with this ruc already exists." ], "code": [ "user with this code already exists." ], "document_number": [

我有一个JSON:

{
    "username": [
        "A user with that username already exists."
    ],
    "ruc": [
        "user with this ruc already exists."
    ],
    "code": [
        "user with this code already exists."
    ],
    "document_number": [
        "user with this document number already exists."
    ]
}
属性的名称是动态的,JSON的长度也是动态的。 我使用GSON工具创建了一个POJO,但是没有名字,我不能引用它,这就是为什么我收到的myError实例为null。 这是我的POJO

public class MyError {
    public Map<String, Object> message = new HashMap<>();
}
我想为每个项目显示一条消息,如下所示:

for (String key : myError.message.keySet()) {
    Utils.showToast(getBaseContext(), "Message: " + myError.message.get(key) + " Field: " + key);
}

您可以尝试使用以下方法:

Map myError = new Gson().fromJson(s, Map.class);
或者,如果您想使用您的类,json应该如下所示:

{
“信息”:{
“用户名”:[
“具有该用户名的用户已存在。”
],
“ruc”:[
“具有此ruc的用户已存在。”
],
“代码”:[
“具有此代码的用户已存在。”
],
“文件编号”:[
“具有此文档编号的用户已存在。”
]
}

}

考虑使用Gson和java原生JsonParser

Map myError = new Gson().fromJson(s, Map.class);