json 字符串转Object 时出现异常

解决: json 字符串转Object  时出现 java.lang.ClassCastException: net.sf.ezmorph.bean.MorphDynaBean cannot be cast to cashway.test.JsonString 异常的问题。

解决方法:设置JsonConfig 类。

解决方法:public void strToJson(String str){
    
  JSONArray parentJson = JSONArray.fromObject(str);
  //设置类型
  JsonConfig jsonConfig = new JsonConfig();
  jsonConfig.setRootClass(JsonString.class);   //JsonString.class 
        Map<String, Class> classMap = new HashMap<String, Class>(); 
        classMap.put("topic", Sub.class);  //Sub.class 设置jonString类中包含的类
        jsonConfig.setClassMap(classMap);
  
  List<JsonString> persons = (List<JsonString>)JSONArray.toCollection(parentJson, jsonConfig);
  
  

  //遍历列表
  for(JsonString p:persons){
   System.out.println("id"+p.getId() +" name:"+p.getName() );
   List<Sub> li = p.getTopic();
   int len = li.size();
   if(li != null && len > 0){
    for(int i=0;i<len;i++){
     Sub sub = p.getTopic().get(i);
     System.out.println("type:"+sub.getType() +" subType:"+sub.getSubtype());
    }
   }
  }
  
 }

2.pojo

package cashway.test;

public class Sub {
 
 private String type;
 private String subtype;
 
 public String getType() {
  return type;
 }
 public void setType(String type) {
  this.type = type;
 }
 public String getSubtype() {
  return subtype;
 }
 public void setSubtype(String subtype) {
  this.subtype = subtype;
 }

}

扫描二维码关注公众号,回复: 653246 查看本文章

package cashway.test;

import java.util.ArrayList;
import java.util.List;

public class JsonString {
 
 public static final String  STR_JSON= "["
        +"{"
            +"\"id\": \"1\","
            +"\"name\": \"兰州拉面馆\","
            +"\"country\": \"120112\","
            +"\"address\": \"津南区底商B座6号\","
            +"\"ifvalid\": \"True\","
            +"\"updatetime\": \"2013-9-11 15:30:50\","
            +"\"attribute\": \"450\","
            +"\"phone\": \"123456789\","
            +"\"topic\": ["
            +"{"
            +" \"type\": \"fx\","
            +"\"subtype\": \"4\""
            +"}"
            +"]"
            +"},"
            +"{"
            +"\"id\": \"222222\","
            +"\"name\": \"中学食堂\","
            +"\"country\": \"120\","
            +"\"address\": \"大丰堆镇\","
            +" \"ifvalid\": \"False\","
            +"\"updatetime\": \"2015-9-11 15:31:35\","
            +"\"attribute\": \"30\","
            +"\"phone\": \"233344555\""
            +"}"
            +"]";
 
 private String id;
 private String name;
 private String country;
 private String address;
 private String ifvalid;
 private String updatetime;
 private String attribute;
 private String phone;
 private List<Sub> topic = new ArrayList<Sub>();
 
 public String getId() {
  return id;
 }
 public void setId(String id) {
  this.id = id;
 }
 public String getName() {
  return name;
 }
 public void setName(String name) {
  this.name = name;
 }
 public String getCountry() {
  return country;
 }
 public void setCountry(String country) {
  this.country = country;
 }
 public String getAddress() {
  return address;
 }
 public void setAddress(String address) {
  this.address = address;
 }
 public String getIfvalid() {
  return ifvalid;
 }
 public void setIfvalid(String ifvalid) {
  this.ifvalid = ifvalid;
 }
 public String getUpdatetime() {
  return updatetime;
 }
 public void setUpdatetime(String updatetime) {
  this.updatetime = updatetime;
 }
 public String getAttribute() {
  return attribute;
 }
 public void setAttribute(String attribute) {
  this.attribute = attribute;
 }
 public String getPhone() {
  return phone;
 }
 public void setPhone(String phone) {
  this.phone = phone;
 }
 public List<Sub> getTopic() {
  return topic;
 }
 public void setTopic(List<Sub> topic) {
  this.topic = topic;
 }
}


 

猜你喜欢

转载自gjp014.iteye.com/blog/2024852