Android JacksonUtil

(..•˘_˘•..) 版权声明:转载原创文章请以超链接形式请注明原文章出处,尊重作者,尊重原创! (..•˘_˘•..)点击一下广告页面也是对博主的支持哦


恰饭广告




jackson jar包

implementation group: 'com.fasterxml.jackson.core', name: 'jackson-databind', version: '2.10.0'
implementation group: 'com.fasterxml.jackson.core', name: 'jackson-core', version: '2.10.0'
implementation group: 'com.fasterxml.jackson.core', name: 'jackson-annotations', version: '2.10.0'

JacksonUtil.java

package win.smartown.android.app.tablelayout;

import android.util.Log;

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.util.List;

public class JacksonUtil {
    public static <T> T deserialize(String json,Class<T> cls){
        ObjectMapper mapper = new ObjectMapper();
        T t = null;
        try{
            t=mapper.readValue(json, cls);
        }catch (Exception ex){
            return null;
        }
        return t;
    }

    public static <T> List<T> decode(String json,Class<T> cls) {
        ObjectMapper mapper = new ObjectMapper();
        List<T> list;
       try{
           mapper.configure(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY, true);
           mapper.configure( DeserializationFeature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT, true) ;
           mapper.enable(DeserializationFeature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT);
           JavaType javaType = mapper.getTypeFactory().constructParametricType(List.class, cls);
           list=(List<T>)mapper.readValue(json, javaType);
       }
       catch (Exception ex){
           Log.e( "listerror",ex.toString() );
           return null;
       }
        return list;
    }
}

调用

Patient实体

package win.smartown.android.app.tablelayout;

public class Patient {
    private String patientid;
    private String patientname;
    private String gender;

    public String getPatientid() {
        return patientid;
    }

    public void setPatientid(String patientid) {
        this.patientid = patientid;
    }

    public String getPatientname() {
        return patientname;
    }

    public void setPatientname(String patientname) {
        this.patientname = patientname;
    }

    public String getGender() {
        return gender;
    }

    public void setGender(String gender) {
        this.gender = gender;
    }
}

实现

//        String json = "{\"patientid\":\"001\",\"patientname\":\"金科\",\"gender\":\"男\",\"age\":12,\"addr\":\"云南省昆明市盘龙区\",\"lat\":25.109329,\"lng\":102.671206,\"diseasestatusid\":1,\"droneid\":\"w1\",\"weight\":\"25\",\"height\":\"250\",\"headimg\":\"http:\\/\\/localhost:52740\\/user\\/2.jpg\"}";
//        Patient patneint=JacksonUtil.deserialize( json,Patient.class );
//        TextView tv=findViewById( R.id.textView );
//        tv.setText( patneint.getPatientid() );

        String json = "[{\"patientid\":\"001\",\"patientname\":\"金科\",\"gender\":\"男\"},{\"patientid\":\"002\",\"patientname\":\"希拉\",\"gender\":\"女\"}]";
        List<Patient> list=JacksonUtil.decode( json ,Patient.class);
        Toast.makeText(test.this, list.get(1).getPatientid(), Toast.LENGTH_SHORT).show();

原文链接:https://www.idaobin.com/archives/2166.html

不打赏,点击广告页面 也是对博主的打赏哦.ヘ( ̄ω ̄ヘ)

支付宝 ——————- 微信
图片加载中图片加载中



恰饭广告

发表评论

电子邮件地址不会被公开。 必填项已用*标注

1 + 9 =