·您现在的位置: 云翼网络 >> 文章中心 >> 网站建设 >> app软件开发 >> Android开发 >> Android中Json数据读取与创建的方法

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

作者:佚名      Android开发编辑:admin      更新时间:2022-07-23

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

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

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

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

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

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

  所以需要我们进行创建,创建方法如下:

     

  创建好assets文件目录以后,在其目录下创建一个Text.json文件。

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

  在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();
         }
       }
     });
   }
 }

以上是通过Android中Json数据读取与创建的方法,希望能够帮助到大家,在实际的项目开发中可以通过Gson(谷歌)Fast-Json(阿里巴巴)这两款Json处理API。