Android 知识点及效果

json 解析例子

2019-03-30  本文已影响14人  穿越平行宇宙

这是json解析的一个小例子,望你们好好理解,升上小实训!好好加油包,骚年们!

json句子:

[
         {
             "name": "“给我唱首歌”\n“给我讲个故事”"
         },

         {
             "name": "“我想订机票”\n“给我说个新闻”"
         },

         {
             "name": "“给我讲个笑话”\n“来一首李白的静夜思”"
         },

         {
             "name": "“看一下去上海的航班”\n“百科搜索张学友”"
         },

         {
             "name": "“如果嫌我太烦”\n可以对我说“休息去吧”"
         }
]

implementation 'com.google.code.gson:gson:2.2.4'//gson
implementation 'com.android.support:design:27.1.1' //防止版本错误

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/rlv"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

    </android.support.v7.widget.RecyclerView>

</android.support.constraint.ConstraintLayout>

适配器中的条目布局:
recycler_item.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <TextView
        android:id="@+id/tv_text"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="20dp"
        android:text="@string/app_name" />

    <View
        android:layout_width="match_parent"
        android:layout_height="3dp"
        android:background="@color/colorPrimary" />

</LinearLayout>

bean包下的:
NameBean.java

public class NameBean {

    /**
     * name : “给我唱首歌”
     “给我讲个故事”
     */

    private String name;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

adapter包下的:
HomeAdapter.java

public class HomeAdapter extends RecyclerView.Adapter<HomeAdapter.ViewHolder> {

    private Context mContext;
    private List<NameBean> mData;

    public HomeAdapter(Context context, List<NameBean> data) {
        mContext = context;
        mData = data;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {

        View inflate = LayoutInflater.from(mContext).inflate(R.layout.recycler_item, null);

        return new ViewHolder(inflate);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {

        NameBean nameBean = mData.get(position);
        holder.mText.setText(nameBean.getName());

    }

    @Override
    public int getItemCount() {
        return mData.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {

        private final TextView mText;

        public ViewHolder(View itemView) {
            super(itemView);
            mText = itemView.findViewById(R.id.tv_text);
        }
    }
}

activity包下的:
MainActivity.java

/*[
        {
        "name": "“给我唱首歌”\n“给我讲个故事”"
        },
        {
        "name": "“我想订机票”\n“给我说个新闻”"
        },
        {
        "name": "“给我讲个笑话”\n“来一首李白的静夜思”"
        },
        {
        "name": "“看一下去上海的航班”\n“百科搜索张学友”"
        },
        {
        "name": "“如果嫌我太烦”\n可以对我说“休息去吧”"
        }
]*/


/*
1. 对象的序列化和反序列化

        Type listType = new TypeToken<List<String>>() {}.getType();
        List<String> target = new LinkedList<String>();
        target.add("blah");
        Gson gson = new Gson();
        String json = gson.toJson(target, listType);
        List<String> target2 = gson.fromJson(json, listType);
*/

public class MainActivity extends AppCompatActivity {

    private RecyclerView mRlv;
    private static final String TAG = "MainActivity";
    private List<NameBean> mList1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initData();
        initView();
    }

    private void initData() {

        // 创建Gson解析
        Gson gson = new GsonBuilder().serializeNulls().create();

        // 设置TypeToken
        Type type = new TypeToken<List<NameBean>>() {
        }.getType();

        // 创建List<NameBean>集合
        List<NameBean> list = new ArrayList<>();

        // 设置数据
        List<String> name = new ArrayList<>();
        name.add("“给我唱首歌”\\n“给我讲个故事”");
        name.add("“我想订机票”\\n“给我说个新闻”");
        name.add("“给我讲个笑话”\\n“来一首李白的静夜思”");
        name.add("“看一下去上海的航班”\\n“百科搜索张学友”");
        name.add("“如果嫌我太烦”\\n可以对我说“休息去吧”");

        // 循环添加数据
        for (int i = 0; i < name.size(); i++) {
            NameBean nameBean = new NameBean();
            nameBean.setName(name.get(i));
            list.add(nameBean);
        }

        // 获取解析的字符串
        String s = gson.toJson(list, type);

        // 解析出来的集合
        mList1 = gson.fromJson(s, type);


    }

    private void initView() {
        // 获取控件对象
        mRlv = (RecyclerView) findViewById(R.id.rlv);

        // 设置显示方式
        mRlv.setLayoutManager(new LinearLayoutManager(this));

        // 设置适配器
        HomeAdapter adapter = new HomeAdapter(this, mList1);
        mRlv.setAdapter(adapter);

    }

}
上一篇下一篇

猜你喜欢

热点阅读