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
Java 如何将json字符串反序列化为对象_Java_Gson - Fatal编程技术网

Java 如何将json字符串反序列化为对象

Java 如何将json字符串反序列化为对象,java,gson,Java,Gson,如何将上述字符串反序列化为java对象 我使用的类是 { "LocalLocationId [id=1]":{ "type":"folderlocation", "id":{ "type":"locallocationid", "id":1 }, "parentId":{ "type":"locallocationid", "id":0 }, "n

如何将上述字符串反序列化为java对象

我使用的类是

{
   "LocalLocationId [id=1]":{
      "type":"folderlocation",
      "id":{
         "type":"locallocationid",
         "id":1
      },
      "parentId":{
         "type":"locallocationid",
         "id":0
      },
      "name":"Test",
      "accessibleToUser":true,
      "defaultLocation":false,
      "timezoneId":"Asia/Calcutta",
      "children":[]
   },
   "LocalLocationId [id=0]":{
      "type":"folderlocation",
      "id":{
         "type":"locallocationid",
         "id":0
      },
      "parentId":null,
      "name":"Locations",
      "accessibleToUser":false,
      "defaultLocation":false,
      "timezoneId":"Asia/Calcutta",
      "children":[{
         "type":"locallocationid",
         "id":1
      }]
   },
   "allAllowedChildren":[{
      "type":"locallocationid",
      "id":1
   }]
}
公共类树{
@SerializedName(“allAllowedChildren”)
所有被允许的儿童的私人名单;
@SerializedName(“LocalLocationId”)
私有地图localLocationId;
公共类LocalLocationId{
@序列化名称(“类型”)
私有字符串类型;
@序列化名称(“名称”)
私有字符串名称;
@SerializedName(“accessibleToUser”)
私有布尔可访问用户;
@SerializedName(“默认位置”)
私密位置;
@SerializedName(“timezoneId”)
私有字符串时区ID;
@序列化名称(“id”)
私人身份证;
@SerializedName(“父ID”)
私人Id parentId;
@序列化名称(“子项”)
私人名单儿童;
公共字符串getType(){
返回类型;
}
公共字符串getName(){
返回名称;
}
公共布尔值isAccessibleToUser(){
返回可访问用户;
}
公共布尔值isDefaultLocation(){
返回默认位置;
}
公共字符串getTimezoneId(){
返回时区ID;
}
公共Id getId(){
返回id;
}
公共Id getParentId(){
返回parentId;
}
公共列表getChildren(){
返回儿童;
}
}
公共类Id{
私有字符串类型;
私有整数id;
公共字符串getType(){
返回类型;
}
公共整数getId(){
返回id;
}
}
公共列表GetAllowedChildren(){
归还所有被允许的儿童;
}
公共映射getLocalLocationId(){
返回localLocationId;
}
}

用户JSONParser哪一个更快

下面是示例。如果你用谷歌搜索,可能会有一个btter的例子。希望这有帮助

public class Tree {

    @SerializedName("allAllowedChildren")
    private List<Id> allAllowedChildren;

    @SerializedName("LocalLocationId")
    private Map<String, LocalLocationId> localLocationId;

    public class LocalLocationId {
        @SerializedName("type")
        private String type;

        @SerializedName("name")
        private String name;

        @SerializedName("accessibleToUser")
        private boolean accessibleToUser;

        @SerializedName("defaultLocation")
        private boolean defaultLocation;

        @SerializedName("timezoneId")
        private String timezoneId;

        @SerializedName("id")
        private Id id;

        @SerializedName("parentId")
        private Id parentId;

        @SerializedName("children")
        private List<Id> children;

        public String getType() {
            return type;
        }
        public String getName() {
            return name;
        }
        public boolean isAccessibleToUser() {
            return accessibleToUser;
        }
        public boolean isDefaultLocation() {
            return defaultLocation;
        }
        public String getTimezoneId() {
            return timezoneId;
        }
        public Id getId() {
            return id;
        }
        public Id getParentId() {
            return parentId;
        }
        public List<Id> getChildren() {
            return children;
        }
    }

    public class Id {
        private String type;
        private Integer id;

        public String getType() {
            return type;
        }
        public Integer getId() {
            return id;
        }
    }

    public List<Id> getAllAllowedChildren() {
        return allAllowedChildren;
    }
    public Map<String, LocalLocationId> getLocalLocationId() {
        return localLocationId;
    }
}
你可以使用Gson

JSONParser parser=new JSONParser();
System.out.println("=======decode=======");
String s="[0,{\"1\":{\"2\":{\"3\":{\"4\":[5,{\"6\":7}]}}}}]";  
Object obj=parser.parse(s);  
JSONArray array=(JSONArray)obj;  
System.out.println("======the 2nd element of array======");  
System.out.println(array.get(1));  
System.out.println();                  
JSONObject obj2=(JSONObject)array.get(1);  
System.out.println("======field \"1\"==========");  
System.out.println(obj2.get("1"));                      
s="{}";  
obj=parser.parse(s);  
System.out.println(obj);                  
s="[5,]";  
obj=parser.parse(s);  
System.out.println(obj);                  
s="[5,,2]";  
obj=parser.parse(s);  
System.out.println(obj);
@凯达尔

我假设您可以控制JSON输入字符串的创建方式。 我认为对于映射类型的默认GSON反序列化,JSON字符串的格式不正确

我已经修改了输入字符串以供您考虑,这将导致一个非空的LocalLocationId

String json = "Your json string "
Tree treeObj= new Gson().fromJson(json, Tree .class);
如果我对输入字符串的假设不正确,请发表评论

编辑1: 由于输入不能修改,请考虑编写自定义反序列化器。 下面是注册自定义反序列化类的方法

{
   "LocalLocationId":[
   [
     "1",
       {
          "type":"folderlocation",
          "id":{
             "type":"locallocationid",
             "id":1
          },
          "parentId":{
             "type":"locallocationid",
             "id":0
          },
          "name":"Test",
          "accessibleToUser":true,
          "defaultLocation":false,
          "timezoneId":"Asia/Calcutta",
          "children":[]
       }
   ],
   [
     "2",
       {
          "type":"folderlocation",
          "id":{
             "type":"locallocationid",
             "id":0
          },
          "parentId":null,
          "name":"Locations",
          "accessibleToUser":false,
          "defaultLocation":false,
          "timezoneId":"Asia/Calcutta",
          "children":[{
             "type":"locallocationid",
             "id":1
          }]
       }
   ]
   ],
   "allAllowedChildren":[{
      "type":"locallocationid",
      "id":1
   }]
}
下面是TreedSerializer

GsonBuilder gsonb = new GsonBuilder();
        gsonb.registerTypeAdapter(Tree.class, new TreeDeserializer());
        Gson gson = gsonb.create();

我将TODOs放在反序列化程序中,您需要编写自定义代码,将反序列化的值注入刚刚创建的树类中。希望这有帮助。无法提供完整的实现,但我认为这将是部分解决方案

您可以使用Jackson提供的
ObjectMapper
-

LocalLocationId [id=1]
org.test.StackOverflowAnswers.Tree$LocalLocationId@464bee09
LocalLocationId [id=0]
org.test.StackOverflowAnswers.Tree$LocalLocationId@f6c48ac
[{type=locallocationid, id=1.0}]
org.test.StackOverflowAnswers.Tree@589838eb

您遇到了什么错误?对于您现在使用的类,将上面的字符串反序列化为java对象是不可能的。@RyanFung想详细说明一下吗?我猜OP不知道为什么会这样。@dotvav.,在
Tree-Tree=gson.fromJson(locationTree,Tree.class)之后
我将LocalLocationId设置为
null
@RyanFung,如果可以更改所需的树类结构,请将上述结果转换为
null
,因为LocalLocationId mapI可以看到您需要修改json字符串,其格式不正确。json字符串是从第三方api接收的。,这正是我提到的格式。那么我可以看到唯一的选择是编写自己的代码来执行部分反序列化。我已经做了一些类似的事情,将字符串解析为
类。。我一定会试试你的方法。。干杯
LocalLocationId [id=1]
org.test.StackOverflowAnswers.Tree$LocalLocationId@464bee09
LocalLocationId [id=0]
org.test.StackOverflowAnswers.Tree$LocalLocationId@f6c48ac
[{type=locallocationid, id=1.0}]
org.test.StackOverflowAnswers.Tree@589838eb
Tree deserializedTree = new ObjectMapper().readValue(jsonStringOfTree, Tree.class);