Android中Json数据读取与建立的方法

转自:http://www.jb51.net/article/70875.htmjava

首先介绍下JSON的定义,JSON是JavaScript Object Notation的缩写。android

一种轻量级的数据交换格式,具备良好的可读和便于快速编写的特性。业内主流技术为其提供了完整的解决方案(有点相似于正则表达式,得到了当今大部分语言的支持),从而能够在不一样平台间进行数据交换。JSON采用兼容性很高的文本格式,同时也具有相似于C语言体系的行为。
正则表达式

JSON的结构:
(1) Name/Value Pairs(无序的):相似所熟知的Keyed list、 Hash table、Disctionary和Associative array。在Android平台中同时存在另一个类 "Bundle",某种程度上具备类似的行为。json

(2) Array(有序的):一组有序的数据列表。app

一:  Json的特性和在数据交互中的地位就不用说了,直接看案例。eclipse

  首先在android studio中建立assets文件目录,用于存放Json数据文件,android studio 1.3 默认项目文件目录下是没有assets文件夹的,ide

  因此须要咱们进行建立,建立方法以下:ui

     

 


  建立好assets文件目录之后,在其目录下建立一个Text.json文件。this

二:如何得到assets文件目录下的Json数据:spa

  在eclipse下是:InputStreamReader(getAssets().open("Text.json"),"UTF-8");得到该文件数据,并以InputStream返回数据。

  而在android studio则是经过:JsonLearn.this.getClass().getClassLoader().getResourceAsStream("assets/" + "Text.json");返回相应InputStream.

三:案例展现:

  1:案例项目app界面以下,经过按钮分别实现Json数据的读取和建立,并展现在TextView中。

 

 

  2:代码以下:

package activity.cyq.datalrearn;
import android.support.v.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
public class JsonLearn extends AppCompatActivity {
  private TextView writeText, readText;
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_json_learn);
    readText = (TextView) findViewById(R.id.readJsonText);
    writeText = (TextView) findViewById(R.id.writeJsonText);
    /*读取Json数据*/
    findViewById(R.id.readJsioBtn).setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v) {
        /*获取到assets文件下的TExt.json文件的数据,并以输出流形式返回。*/
        InputStream is = JsonLearn.this.getClass().getClassLoader().getResourceAsStream("assets/" + "Text.json");
        InputStreamReader streamReader = new InputStreamReader(is);
        BufferedReader reader = new BufferedReader(streamReader);
        String line;
        StringBuilder stringBuilder = new StringBuilder();
        try {
          while ((line = reader.readLine()) != null) {
            // stringBuilder.append(line);
            stringBuilder.append(line);
          }
          reader.close();
          reader.close();
          is.close();
        } catch (IOException e) {
          e.printStackTrace();
        }
        try {
          JSONObject person = new JSONObject(stringBuilder.toString());
          JSONArray infArray = person.getJSONArray("inf");
          for (int i = ; i < infArray.length(); i++) {
            JSONObject inf_Array = infArray.getJSONObject(i);
            readText.append("name:" + inf_Array.getString("name") + "\n");
            readText.append("IdCard:" + inf_Array.getString("IdCard"));
            readText.append("age:" + inf_Array.getInt("age"));
            readText.append("married:" + inf_Array.getBoolean("married"));
          }
        } catch (JSONException e) {
          e.printStackTrace();
        }
      }
    });
    /*建立Json数据并显示*/
    findViewById(R.id.writeJsioBtn).setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v) {
        try {
          JSONObject inf = new JSONObject();
          inf.put("number", );
          JSONArray array = new JSONArray();
          JSONObject arr_ = new JSONObject();
          arr_.put("name", "张三");
          arr_.put("age", );
          arr_.put("IdCard", "XC");
          arr_.put("married", true);
          JSONObject arr_ = new JSONObject();
          arr_.put("name", "李四");
          arr_.put("age", );
          arr_.put("IdCard", "@DC");
          arr_.put("married", true);
          array.put(, arr_);
          array.put(, arr_);
          inf.put("inf", array);
          writeText.setText(inf.toString());
        } catch (JSONException e) {
          e.printStackTrace();
        }
      }
    });
  }
}
相关文章
相关标签/搜索