如何将json字符串转换为java对象

时间:2013-06-15 04:05:21

标签: java json serialization deserialization

{
  "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
}

这是我的json字符串。我存储在字符串myjsonString中,Approbvall_BE是我的类。这是我的代码。

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

这条线之后甚至s.o.p本身也无法正常工作。任何人都可以帮助我吗?。

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

3 个答案:

答案 0 :(得分:1)

使用jsonschema2pojo为您的JSON生成模型,并使用相同的代码将其deserilize到Java对象

答案 1 :(得分:0)

    String str = "Your json string";

JSONObject json = new JSONObject(str);

答案 2 :(得分:0)

import org.codehaus.jackson.map.ObjectMapper;

在readvalue方法中传递json

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

这会将json转换为java