精华内容
下载资源
问答
  • JSONObject 实体类

    千次阅读 2020-05-25 16:07:03
    //Json字符串 JSONObject jsonData = JSONObject.parseObject(loginData); //返回信息的处理集合 JSONObject jo = new JSONObject();

    Json格式转字符串

     {"Data":{"name":"xxx" , "age":20 }}
    

    JSONObject jsonData = JSONObject.parseObject(Data);

    json集合

    JSONObject json = new JSONObject();

    在返回的json里面获取date信息

    {"Data":{"name":"xxx" , "age":20 },"success":{"code":"0" , "msg":"成功"}}
    

    JSONObject jsonObject =
    JSON.parseObject(String.valueOf(xxx.get(“data”)));

    获取data里面的某个参数

    String name= jsonObject.getString("name");
    

    将json转成实体类,(前提是–字段名字对应上)

     {"Company":{"name":"xxx" , "age":""20" },{"name":"xxx" , "age":""20" }}
    

    实体类:

    public class Company {
    private String name;
    private String age;
    }
    

    Company company =
    JSONObject.toJavaObject(JSONObject.parseObject(JSONObject.toJSONString(paramsMap)),Company.class);

    map 转 json对象

    JSONObject jsonObject =
    JSONObject.parseObject(JSONObject.toJSONString(map));
    // list
    List list = (List)jsonObject.get(“powerPlantIdList”);

    展开全文
  • 使用fastjson 进行jsonObject转实体类对象 1 <dependency> 2 <groupId>com.alibaba</groupId> 3 <artifactId>fastjson</artifactId> 4 ...

    使用fastjson 进行jsonObject转实体类对象

     
    1        <dependency>
    2             <groupId>com.alibaba</groupId>
    3             <artifactId>fastjson</artifactId>
    4             <version>1.2.7</version>
    5         </dependency>

    首先引入相关jar包,

    假设有一个实体类User

    public class User{
      private int id;
      private String name;    
    }

    然后通过fastJson进行类型转换

    public static void main(String[] args){
        String userString = "{"id":1,"name","lz"}";
        JSONObject userJson = JSONObject.parseObject(userString);
       User user = JSON.toJavaObject(userJson,User.class);
       
    }

     

    转载于:https://www.cnblogs.com/achengmu/p/11310730.html

    展开全文
  • 导入jar依赖 <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.7<...将json转换为实体对象 import co

    导入jar依赖

            <dependency>
                 <groupId>com.alibaba</groupId>
                 <artifactId>fastjson</artifactId>
                 <version>1.2.7</version>
             </dependency>

    将json转换为实体对象

    import com.alibaba.fastjson.JSON;
    import com.alibaba.fastjson.JSONObject;
    
     class User{
        private int id;
        private String name;
    
         public int getId() {
             return id;
         }
    
         public void setId(int id) {
             this.id = id;
         }
    
         public String getName() {
             return name;
         }
    
         public void setName(String name) {
             this.name = name;
         }
    
         @Override
         public String toString() {
             return "User{" +
                     "id=" + id +
                     ", name='" + name + '\'' +
                     '}';
         }
     }
    
    public class mymain {
        public static void main (String[]args){
            String userString = "{\"id\":1,\"name\":\"lz\"}";
            JSONObject userJson = JSONObject.parseObject(userString);
            User user = JSON.toJavaObject(userJson, User.class);
            System.out.println(user.toString());
    
        }
    }

    结果:

     

    思考1: 如果一个json中有重复属性 会如何?

    如:{"id":1,"name":"lz","name":"wjw"}

    答:会覆盖

    解析成list:

    fastjson解析以{} 为一个对象进行解析。一个字符串包含n个{}就会生成n个对象

    展开全文
  • 另见:json 日期出错,JSONArray.toCollection 时间总是转化为当前时间 net.sf.json 这个包还真是存在不少问题。 上代码: package net.sf.ezmorph.object; import java.text.ParseException; import java....

    另见:json 转日期出错,JSONArray.toCollection 时间总是转化为当前时间

    net.sf.json 这个包还真是存在不少问题。

    上代码:

    package net.sf.ezmorph.object;
    
    import java.text.ParseException;
    import java.text.SimpleDateFormat;
    import java.util.Date;
    import java.util.Locale;
    
    import net.sf.ezmorph.MorphException;
    
    import org.apache.commons.lang.builder.EqualsBuilder;
    import org.apache.commons.lang.builder.HashCodeBuilder;
    
    /**
     * Morphs a String to a Date.<br>
     * <p>
     * This morpher will iterate through the supplied formats until one succeeds or
     * the default value is returned (if default value is configured).
     * </p>
     *
     * @author Andres Almiray <aalmiray@users.sourceforge.net>
     */
    public final class DateMorpherEx extends AbstractObjectMorpher {
    	private Date defaultValue;
    	private String[] formats;
    	private boolean lenient;
    	private Locale locale;
    
    	/**
    	 * @param formats
    	 *            a list of formats this morpher supports.
    	 */
    	public DateMorpherEx(String[] formats) {
    		this(formats, Locale.getDefault(), false);
    	}
    
    	/**
    	 * @param formats
    	 *            a list of formats this morpher supports.
    	 * @param lenient
    	 *            if the parsing should be lenient or not.
    	 */
    	public DateMorpherEx(String[] formats, boolean lenient) {
    		this(formats, Locale.getDefault(), lenient);
    	}
    
    	/**
    	 * @param formats
    	 *            a list of formats this morpher supports.
    	 * @param defaultValue
    	 *            return value if the value to be morphed is null.
    	 */
    	public DateMorpherEx(String[] formats, Date defaultValue) {
    		this(formats, defaultValue, Locale.getDefault(), false);
    	}
    
    	/**
    	 * @param formats
    	 *            a list of formats this morpher supports.
    	 * @param defaultValue
    	 *            return value if the value to be morphed is null.
    	 * @param locale
    	 *            the Locale used to parse each format.
    	 * @param lenient
    	 *            if the parsing should be lenient or not.
    	 */
    	public DateMorpherEx(String[] formats, Date defaultValue, Locale locale, boolean lenient) {
    		super(true);
    		if (formats == null || formats.length == 0) {
    			throw new MorphException("invalid array of formats");
    		}
    		// should use defensive copying ?
    		this.formats = formats;
    
    		if (locale == null) {
    			this.locale = Locale.getDefault();
    		} else {
    			this.locale = locale;
    		}
    
    		this.lenient = lenient;
    		setDefaultValue(defaultValue);
    	}
    
    	/**
    	 * @param formats
    	 *            a list of formats this morpher supports.
    	 * @param locale
    	 *            the Locale used to parse each format.
    	 */
    	public DateMorpherEx(String[] formats, Locale locale) {
    		this(formats, locale, false);
    	}
    
    	/**
    	 * @param formats
    	 *            a list of formats this morpher supports.
    	 * @param locale
    	 *            the Locale used to parse each format.
    	 * @param lenient
    	 *            if the parsing should be lenient or not.
    	 */
    	public DateMorpherEx(String[] formats, Locale locale, boolean lenient) {
    		if (formats == null || formats.length == 0) {
    			throw new MorphException("invalid array of formats");
    		}
    		// should use defensive copying ?
    		this.formats = formats;
    
    		if (locale == null) {
    			this.locale = Locale.getDefault();
    		} else {
    			this.locale = locale;
    		}
    
    		this.lenient = lenient;
    	}
    
    	public boolean equals(Object obj) {
    		if (this == obj) {
    			return true;
    		}
    		if (obj == null) {
    			return false;
    		}
    
    		if (!(obj instanceof DateMorpherEx)) {
    			return false;
    		}
    
    		DateMorpherEx other = (DateMorpherEx) obj;
    		EqualsBuilder builder = new EqualsBuilder();
    		builder.append(formats, other.formats);
    		builder.append(locale, other.locale);
    		builder.append(lenient, other.lenient);
    		if (isUseDefault() && other.isUseDefault()) {
    			builder.append(getDefaultValue(), other.getDefaultValue());
    			return builder.isEquals();
    		} else if (!isUseDefault() && !other.isUseDefault()) {
    			return builder.isEquals();
    		} else {
    			return false;
    		}
    	}
    
    	/**
    	 * Returns the default value for this Morpher.
    	 */
    	public Date getDefaultValue() {
    		if (null != defaultValue){
    		return (Date) defaultValue.clone();
    		}
    		return null;
    	}
    
    	public int hashCode() {
    		HashCodeBuilder builder = new HashCodeBuilder();
    		builder.append(formats);
    		builder.append(locale);
    		builder.append(lenient);
    		if (isUseDefault()) {
    			builder.append(getDefaultValue());
    		}
    		return builder.toHashCode();
    	}
    
    	public Object morph(Object value) {
    		if (value == null) {
    			return null;
    		}
    
    		if (Date.class.isAssignableFrom(value.getClass())) {
    			return (Date) value;
    		}
    
    		if (!supports(value.getClass())) {
    			throw new MorphException(value.getClass() + " is not supported");
    		}
    
    		String strValue = (String) value;
    		strValue = strValue.trim();
    		if (0 == strValue.length()){
    			if (isUseDefault()) {
    				return defaultValue;
    			} else {
    				throw new MorphException("Unable to parse the date " + value);
    			}
    		}
    		
    		SimpleDateFormat dateParser = null;
    
    		for (int i = 0; i < formats.length; i++) {
    			if (dateParser == null) {
    				dateParser = new SimpleDateFormat(formats[i], locale);
    			} else {
    				dateParser.applyPattern(formats[i]);
    			}
    			dateParser.setLenient(lenient);
    			try {
    				return dateParser.parse(strValue.toLowerCase());
    			} catch (ParseException pe) {
    				// ignore exception, try the next format
    			}
    		}
    
    		// unable to parse the date
    		if (isUseDefault()) {
    			return defaultValue;
    		} else {
    			throw new MorphException("Unable to parse the date " + value);
    		}
    	}
    
    	public Class morphsTo() {
    		return Date.class;
    	}
    
    	/**
    	 * Sets the defaultValue to use if the value to be morphed is null.
    	 *
    	 * @param defaultValue
    	 *            return value if the value to be morphed is null
    	 */
    	public void setDefaultValue(Date defaultValue) {
    		if (null != defaultValue) {
    			this.defaultValue = (Date) defaultValue.clone();
    		}
    		this.defaultValue = null;
    	}
    
    	public boolean supports(Class clazz) {
    		return String.class.isAssignableFrom(clazz);
    	}
    }

    这里面主要是对 setDefaultValue 和 getDefaultValue 进行修改,使其支持 null 值。

    还有就是 morph 方法,感觉还有很大的优化空间。

    展开全文
  • 在自己需要转实体类的代码之前加上下面这个话就可以了 JSONUtils.getMorpherRegistry() .registerMorpher( new DateMorpherEx(new String[] { "yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd", "yyyy-MM-dd't'HH:mm:ss...
  • JSONObject转Java实体类

    2021-01-21 17:24:55
    JSONObject jsonObject = JSONObject.parseObject("需要的值"); Token token= JSON.toJavaObject(jsonObject,Token.class);
  • JSONObject跟java List转换: JSONObject -->> JSONArray  jsonObject.getJSONArray("list") JSONArray -->... ListJSONArray  List<T> list = new ArrayList<T>();  JSONArray
  • 不同数据类型间的相互转换方法 ... import com.alibaba.fastjson.JSONObject;... // 实体类转String String data = JSONObject.toJSONString(entity); // String转JsonObject JSONObject jsonObject = JSON.pars
  • JSONObject对象转实体类

    千次阅读 2019-04-19 09:42:52
    JSONObject pwkx = JSONObject.fromObject(pwkxx); PWK pwk=(PWK )JSONObject.toBean(pwkx, PWK .class);
  • <p>2、service的方法定义的传参是要实体类还是直接用jsonObject呢? <p>3、测过效率问题,差别也不大 <p>4、考虑过实体类可以定义一些方法,在转换的时候就可以进行一些简单处理,以及...
  • public class MainTest { public static void main(String[] args) { //创建JSONOobject JSONObject json; //创建测试Bean People people = new People(); people.setId(12); people.setName("王...
  • 使用的是com.alibaba.fastjson.JSONObject ...JSONObject的maven依赖: <!-- https://mvnrepository.com/artifact/com.alibaba/fastjson --> <dependency> <groupId>com.alibaba</g...
  • 1、String转JSONObject 前言:String 是JSONObject格式的字符串 eg: JSONObject jSONObject = JSONObject.parseObject(String); 2、StringJSONArray 前言:String 是JSONArray格式的字符串 eg: JSONArray ...
  • JSONObject,JSONArray与java实体类相互转换 正常情况下: JSONObject转换为实体类: Employee employee = JSONObject.toJavaObject(param, Employee.class); JSONArray转换为实体类数组: List employees = JSON....

空空如也

空空如也

1 2 3 4 5 ... 12
收藏数 233
精华内容 93
关键字:

jsonobject转实体类