2016-04-27 14 views
0

私は本当にイムは、次のJSONデシリアライズ、ここに助けが必要:私はのInputStream正しいJson逆シリアル化をJavaオブジェクトにする方法は?

として、それをrecive JSONはまた、私は文字列で試してみましたrecive時に、Webサービスを作成するために、ジャージーを使用して

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

イムを

@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 > "; 
} 

これらのコードはどちらも動作しません。彼らは例外をスローしたり、 "All done"をプリントしたりしません。
オブジェクトをデバッグすると、userは変数メニューに表示されません。
私の経験では、エラーがラインで起こっているためですUserInformation user = new Gson().fromJson(isr, UserInformation.class);

しかし、私はどの1つが表示されますか。

私のユーザ情報クラスは、私はあなたがGoogle gsonを使用していると仮定しています次の

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 
} 
+0

'User'または' UserInformation' ? – Savior

+0

'inStr'とは何ですか? – Savior

+0

例外のスタックトレースを貼り付けてください。 –

答えて

1

です。ここに私の答えは次のとおりです。

@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(); 


    } 
+0

あなたは正しいです...私の最初の考えは、オブジェクトを返すことでした: String s = gson.toJson( "all done"); return Response.status(Response.Status.OK).entity(s)。ビルド(); 私の心が変わり、コードを掃除するのを忘れました – mrseanpaul81

+0

ありがとう、それは私が価値を得ることを解決するのに役立ちます。しかし、doestはどのようにJavaオブジェクトに変換する問題を解決します。 – mavi

0

私はすでに解決策を見つける、問題は、私は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(); 
     } 

    } 
関連する問題