簡體   English   中英

如何對Java對象進行正確的Json反序列化?

[英]How to make a correct Json Deserialization to a Java Object?

我在這里確實需要幫助,即時反序列化下一個json:

{
    "name":"myname",
    "userID":"12345",
    "password":"sha1passwordASDESFSGSD",
    "active":"1",
    "profile":"2",
    "job":"Manager"
}

我使用澤西島創建webService,當我接收到json時我將其接收為InputStream

我也嘗試過一個字符串

@POST
@Path("user/in/")
@Produces("application/json")
public String InsertUser(InputStream inStr) 
{
    String line = null, res = "POST 200 > ";
    BufferedReader br = new BufferedReader(new InputStreamReader(inStr));
    try {
        while ((line = br.readLine()) != null) {
            System.out.println(line);
            res += line;
        }
    } catch (IOException e) {
        e.printStackTrace();
    }

    try {
        UserInformation user = new Gson().fromJson(res, UserInformation.class);
        System.out.println("All done");
        System.out.println(user.getName());

    } catch (Exception e) {
        System.out.println("Error al convertir jo object " + e.getCause() + e.getMessage());
    }

    return "POST 200 > ";
}

我嘗試使用InputStreamReader:

@POST
@Path("user/in/")
@Produces("application/json")
public String InsertUser(InputStream inStr)
{
    try {
        InputStreamReader isr = new InputStreamReader(inStr);
        UserInformation user = new Gson().fromJson(isr, UserInformation.class);
        System.out.println("All done");

        System.out.println(user.getName());

    } catch (Exception e) {
        System.out.println("Error al convertir jo object " + e.getCause() + e.getMessage());
    }

    return "POST 200 > ";
}

這些代碼都不起作用。 他們不會拋出異常或打印“全部完成”。
當我調試對象時, user不會出現在變量菜單中。
以我的經驗,是因為UserInformation user = new Gson().fromJson(isr, UserInformation.class);行中發生錯誤UserInformation user = new Gson().fromJson(isr, UserInformation.class);

但是我看不到是哪一個。

我的UserInformation類是下一個

public class UserInformation {

    private String name;
    private String userID;
    private String password;
    private String active;
    private String profile;
    private String job;

    // Methods removed for brevity
}

我假設您正在使用Google gson。 這是我的答案:

    @Post
    @Path("user/in")
    @Produces(MediaType.APPLICATION_JSON)
    public Response InsertUser(string json){
      JsonElement jsonElement = new JsonParser().parse(json);
      JsonObject object = jsonElement.getAsJsonObject();
      String name = object.getAsJsonPrimitive("name").getAsString();
      int userID = object.getAsJsonPrimitve("userID").getAsInt();
      String password = object.getAsJsonPrimitive("password").getAsString();
      String job = object.getAsJsonPrimitive("job").getAsString();
      int active = object.getAsJsonPrimitve("active").getAsInt();
      int profile = object.getAsJsonPrimitve("profile").getAsInt();



      return Response.status(Response.Status.OK).entity("all done").build();


    }

我已經找到了解決方案,問題是我收到了InputStream,按照其建議,解決方案代碼為:

  @POST
  @Path("users/in/")
  @Produces(MediaType.APPLICATION_JSON)
  public String InsertUser(String json)
  { 

      try{
          UserInformation user = new Gson().fromJson(json, UserInformation.class);
          String name = user.getName();
          String userID = user.getUserID();                       
          String password = user.getPassword();
          String job = user.getJob();
          String active = user.getActive();
          String profile = user.getProfile();

      }catch(Exception e){
          System.out.println(Response.status(500).entity(e.getCause() + e.getMessage()).build().toString());
          return Response.status(500).entity(e.getCause() + e.getMessage()).build().toString();
      }

  }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM