当前位置:   article > 正文

Json3种解析方式

json3

1.导包

 implementation 'com.google.code.gson:gson:2.8.6'
 implementation 'com.alibaba:fastjson:1.2.73'
  • 1
  • 2

2.使用

package com.example.imageloader2;

import androidx.appcompat.app.AppCompatActivity;
import android.annotation.SuppressLint;
import android.os.Bundle;
import android.util.Log;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.LinearLayout;

import com.alibaba.fastjson.JSON;
import com.google.gson.Gson;
import com.google.gson.JsonObject;

import org.json.JSONObject;

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

public class ThirdActivity extends AppCompatActivity {
    private static final String TAG = "ThirdActivity";
    private LinearLayout linearLayout;
    String data = "{\n" +
            "  \"paramz\": {\n" +
            "    \"feeds\": [\n" +
            "      {\n" +
            "        \"id\": 299076,\n" +
            "        \"oid\": 288340,\n" +
            "        \"category\": \"article\",\n" +
            "        \"data\": {\n" +
            "          \"subject\": \"荔枝新闻3.0:不止是阅读\",\n" +
            "          \"summary\": \"江苏广电旗下资讯类手机应用“荔枝新闻”于近期推出全新升级换代的3.0版。\",\n" +
            "          \"cover\": \"/Attachs/Article/288340/3e8e2c397c70469f8845fad73aa38165_padmini.JPG\",\n" +
            "          \"pic\": \"\",\n" +
            "          \"format\": \"txt\",\n" +
            "          \"changed\": \"2015-09-22 16:01:41\"\n" +
            "        }\n" +
            "      }\n" +
            "    ],\n" +
            "    \"PageIndex\": 1,\n" +
            "    \"PageSize\": 20,\n" +
            "    \"TotalCount\": 53521,\n" +
            "    \"TotalPage\": 2677\n" +
            "  }\n" +
            "}\n";



    String data2 = "{\n" +
            "    \"homeadlist\": [\n" +
            "        {\n" +
            "            \"id\": 1,\n" +
            "            \"imgurl\": \"/img/homead/a73b3f1d-0f14-429d-9d0f-70643fb0f0eb.jpg\",\n" +
            "            \"jumpflag\": 0,\n" +
            "            \"jumpurl\": \"\",\n" +
            "            \"posflag\": 1,\n" +
            "            \"remark\": \"1111\",\n" +
            "            \"cityid\": 1\n" +
            "        },\n" +
            "        {\n" +
            "            \"id\": 12,\n" +
            "            \"imgurl\": \"/img/homead/eb442fbf-49db-4ba6-a102-d781505f426d.jpg\",\n" +
            "            \"jumpflag\": 0,\n" +
            "            \"jumpurl\": \"\",\n" +
            "            \"posflag\": 2,\n" +
            "            \"remark\": \"\",\n" +
            "            \"cityid\": 1\n" +
            "        },\n" +
            "        {\n" +
            "            \"id\": 14,\n" +
            "            \"imgurl\": \"/img/homead/68109460-635d-4c5c-8be8-64d7c7889d18.jpg\",\n" +
            "            \"jumpflag\": 0,\n" +
            "            \"jumpurl\": \"http://shiranlife.kuaizhan.com/\",\n" +
            "            \"posflag\": 4,\n" +
            "            \"remark\": \"\",\n" +
            "            \"cityid\": 1\n" +
            "        }\n" +
            "    ]\n" +
            "}\n";
    @SuppressLint("SetJavaScriptEnabled")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        linearLayout = new LinearLayout(this);
        WebView webView = new WebView(this);
        WebSettings settings = webView.getSettings();
        //让webView支持JS
        settings.setJavaScriptEnabled(true);
        //加载百度网页
        webView.loadUrl("https://www.baidu.com");
        LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
        lp.setMargins(50, 50, 50, 50);
        webView.setLayoutParams(lp);
        linearLayout.addView(webView);
        setContentView(linearLayout);


        List<HashMap<String, Object>> dataList = JsonUtils.getJsonList(data);
        for(HashMap map : dataList) {
            Log.d(TAG, "subject:"+map.get("subject")+", summary:"+ map.get("summary")+", cover:"+ map.get("cover"));
        }

        List<DataModel> dataList2 =  JsonUtils.getJsonList2(data);
        for(DataModel model : dataList2) {
            Log.d(TAG, "22---subject:"+model.getSubject()+", summary:"+ model.getSummary()+", cover:"+ model.getCover());
        }


        //Gson解析
        Homead homead = new Gson().fromJson(data2, Homead.class);
        Log.d(TAG, "size-333:"+homead.getHomeadlist().size());
        for(Homead.HomeadlistBean bean : homead.getHomeadlist()) {
            Log.d(TAG, "getId:"+ bean.getId());
        }

        //fastJson解析
        Homead homead2 = JSON.parseObject(data2, Homead.class);
        Log.d(TAG, "size-444:"+homead.getHomeadlist().size());
        for(Homead.HomeadlistBean bean : homead.getHomeadlist()) {
            Log.d(TAG, "getId---222:"+ bean.getId());
        }

        Homead.HomeadlistBean bean1 = new Homead.HomeadlistBean(1, "q");
        Homead.HomeadlistBean bean2 = new Homead.HomeadlistBean(2, "w");
        Homead.HomeadlistBean bean3 = new Homead.HomeadlistBean(3, "e");
        Homead.HomeadlistBean bean4 = new Homead.HomeadlistBean(4, "r");
        List<Homead.HomeadlistBean> homeadlist  = new ArrayList<>();
        homeadlist.add(bean1);
        homeadlist.add(bean2);
        homeadlist.add(bean3);
        homeadlist.add(bean4);
        Homead homead1 = new Homead();
        homead1.setHomeadlist(homeadlist);
        String h = JSON.toJSONString(homead1);
        Log.d(TAG, "h---555:"+ h);
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139

3.解析类
1)DataModel

package com.example.imageloader2;

public class DataModel{

    public  String subject;
    public  String summary;
    public  String cover;
    public  String pic;
    public  String format;
    public  String changed;
    public void setSubject(String subject) {
        this.subject = subject;
    }
    public String getSubject() {
        return subject;
    }

    public void setSummary(String summary) {
        this.summary = summary;
    }
    public String getSummary() {
        return summary;
    }

    public void setCover(String cover) {
        this.cover = cover;
    }
    public String getCover() {
        return cover;
    }

    public void setPic(String pic) {
        this.pic = pic;
    }
    public String getPic() {
        return pic;
    }

    public void setFormat(String format) {
        this.format = format;
    }
    public String getFormat() {
        return format;
    }

    public void setChanged(String changed) {
        this.changed = changed;
    }
    public String getChanged() {
        return changed;
    }

}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53

2)Homead

package com.example.imageloader2;

import java.io.Serializable;
import java.util.List;


public class Homead {
    public List<HomeadlistBean> getHomeadlist() {
        return homeadlist;
    }

    private List<HomeadlistBean> homeadlist;

    public void setHomeadlist(List<HomeadlistBean> homeadlist) {
        this.homeadlist = homeadlist;
    }

    public static class HomeadlistBean {
        public HomeadlistBean() {
        }

        public String getRemark() {
            return remark;
        }

        public HomeadlistBean(int id, String remark) {
            this.id = id;
            this.remark = remark;
        }

        public int getId() {
            return id;
        }

        /**
         * id : 1
         * imgurl : /img/homead/a73b3f1d-0f14-429d-9d0f-70643fb0f0eb.jpg
         * jumpflag : 0
         * jumpurl :
         * posflag : 1
         * remark : 1111
         * cityid : 1*/


        private int id;
        private String imgurl;
        private int jumpflag;
        private String jumpurl;
        private int posflag;
        private String remark;
        private int cityid;
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53

4.工具类

package com.example.imageloader2;

import android.util.Log;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

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

public class JsonUtils {
    private static final String TAG = "JsonUtils";

    /**
     * 系统的JSONObject, JSONArray
     */
    public static List<HashMap<String, Object>> getJsonList(String json) {
        List<HashMap<String, Object>> dataList;
        dataList = new ArrayList<>();
        try {
            JSONObject rootObject = new JSONObject(json);
            JSONObject paramzObject = rootObject.getJSONObject("paramz");
            JSONArray feedsArray = paramzObject.getJSONArray("feeds");
            for (int i = 0; i < feedsArray.length(); i++) {
                JSONObject sonObject = feedsArray.getJSONObject(i);
                JSONObject dataObject = sonObject.getJSONObject("data");
                String subjectStr = dataObject.getString("subject");
                String summaryStr = dataObject.getString("summary");
                String coverStr = dataObject.getString("cover");
                HashMap<String, Object> map = new HashMap<>();
                map.put("subject", subjectStr);
                map.put("summary", summaryStr);
                map.put("cover", coverStr);
                dataList.add(map);
            }
            return dataList;
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return null;
    }


    /**
     * Gson解析
     */
    public static List<DataModel> getJsonList2(String json) {
        List<DataModel> dataList;
        dataList = new ArrayList<>();
        try {
            JSONObject rootObject = new JSONObject(json);
            JSONObject paramzObject = rootObject.getJSONObject("paramz");
            JSONArray feedsArray = paramzObject.getJSONArray("feeds");
            for (int i = 0; i < feedsArray.length(); i++) {
                JSONObject sonObject = feedsArray.getJSONObject(i);
                Log.d(TAG, "DATA:"+sonObject.getString("data"));
                DataModel model = new Gson().fromJson(sonObject.getString("data"), new TypeToken<DataModel>(){}.getType());
                dataList.add(model);
            }
            return dataList;
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static Homead getJsonList3(String json) {
        Homead homead = new Gson().fromJson(json, Homead.class);
        return homead;
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/很楠不爱3/article/detail/351698
推荐阅读
相关标签
  

闽ICP备14008679号