简体   繁体   English

如何将JSON字符串转换为Java对象

[英]How to convert json string to java object

{
  "application_num" : 185,
  "age" : 35,
  "mobile_num" : "9943847180",
  "active" : null,
  "photo" : null,
  "bytes" : null,
  "creator" : null,
  "modifier" : null,
  "modifiedby" : null,
  "updatedby" : null,
  "cus_first_name" : "firstname",
  "cus_last_name" : "lastname",
  "sex" : "Male",
  "profession" : "profession",
  "designation" : "designation",
  "work_place_address" : "workkkkkkkkkk",
  "phone_number" : "044222222222",
  "resi_address" : "resssssssssssssi",
  "membership_type" : "Silver",
  "debitbal" : "100022",
  "dueamt" : "1002",
  "renewaldate" : "2013-06-13",
  "photoinputstream" : null
}

this is my json string.I stored in String myjsonString , Approbvall_BE is my class. 这是我的json字符串。我存储在String myjsonStringApprobvall_BE是我的课程。 This is my code. 这是我的代码。

Gson gson=new Gson(); 
//convert the json string back to object
Approval_BE app_be = gson.fromJson(myjsonString, Approval_BE.class);
System.out.println("print"+app_be.cus_first_name);

after this line even sop itself not working. 在这条线之后,甚至自己也无法正常工作。 Can anyone help me?. 谁能帮我?。

Approval_BE app_be = gson.fromJson(myjsonString, Approval_BE.class);

使用jsonschema2pojo生成JSON模型,并使用相同的代码将其反序列化为Java对象

    String str = "Your json string";

JSONObject json =新的JSONObject(str);

import org.codehaus.jackson.map.ObjectMapper; 导入org.codehaus.jackson.map.ObjectMapper;

pass json in readvalue method 在readvalue方法中传递json

User user = new ObjectMapper().readValue(json, User.class); 用户user = new ObjectMapper()。readValue(json,User.class);

this will conver json to java 这将json转换为java

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM