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

import android.util.Log;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.ObjectMapper;

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

public class JacksonUtil {
    static ObjectMapper mapper = new ObjectMapper();
    public static <T> T deserialize(String json,Class<T> cls){
        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) {
        List<T> list=new ArrayList<>(  );
       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=mapper.readValue(json, javaType);
       }
       catch (Exception ex){
           Log.e( "listerror",ex.toString() );
           return null;
       }
        return list;
    }

    public <T> String mapToJson(Map<T,T> map){
        String json="";
        try {
            json = mapper.writeValueAsString(map);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        return json;
    }
}

调用

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

让我恰个饭吧.ヘ( ̄ω ̄ヘ)

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



恰饭广告

发表评论

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

÷ 3 = 3