Android Studio读取和创建Json

JSON的基本语法

1.JSon的四个基本规则
(1)并列的数据之间用逗号(,)分隔
(2)映射用冒号(:)表示
(3)并列数据的集合(数组)用方括号([])表示
(4)映射的集合(对象)用大括号({})表示
2.缺点
(1)要求是unicode,否则会出现乱码
(2)语法过于严禁,必须遵循json语法四个原则
3.数据类型
(1)数据结构:object、array
(2)基本类型:string、number、true、false、null
(3)key必须是String类型的,value是任何基本类型或数据结构

实例代码:

{
    "name":"王小二",
    "age":25.2,
    "birthday":"1990-01-01",
    "school":"蓝翔",
    "major":["理发","挖掘机"],
    "has_girlfriend":false,
    "car":null,
    "house":null,
    "time":{
    "value":30,
    "unit":"分钟"
    }
}

Android Studio创建json文件:

177eab807ff9ce452b0a7cc517de42a0 20180510113422700

(1)在assets目录下新建text.json文件,内容如下:

{
    "languages":[
    {"id":1,"ide":"Eclipse","name":"Java"},
    {"id":2,"ide":"XCode","name":"Swift"},
    {"id":3,"ide":"Visual Studio","name":"C#"},
    ],
    "cat":"it"
}

(2)读取json和创建json

package json.android.com.jsontest;
 
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
 
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;
import java.io.UnsupportedEncodingException;
 
public class MainActivity extends Activity {
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
 
        //Json数据的读写
        try {
//            InputStream is = this.getAssets().open("test.json");//eclipse
            InputStream is = MainActivity.this.getClass().getClassLoader().getResourceAsStream("assets/" + "text.json");//android studio
            BufferedReader bufr = new BufferedReader(new InputStreamReader(is));
            String line;
            StringBuilder builder = new StringBuilder();
            while ((line = bufr.readLine()) != null) {
                builder.append(line);
            }
            is.close();
            bufr.close();
 
            try {
                JSONObject root = new JSONObject(builder.toString());
                Log.d("info","cat=" + root.getString("cat"));
                JSONArray array = root.getJSONArray("languages");
                for (int i = 0; i < array.length(); i++) {
                    JSONObject lan = array.getJSONObject(i);
                    Log.d("info","-----------------------");
                    Log.d("info","id=" + lan.getInt("id"));
                    Log.d("info","ide=" + lan.getString("ide"));
                    Log.d("info","name=" + lan.getString("name"));
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
 
 
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
 
        //Json数据的创建
        try {
            JSONObject root = new JSONObject();
            root.put("cat", "it");
 
            JSONObject lan1 = new JSONObject();
            lan1.put("id", 1);
            lan1.put("ide", "Eclipse");
            lan1.put("name", "Java");
 
            JSONObject lan2 = new JSONObject();
            lan2.put("id", 2);
            lan2.put("ide", "XCode");
            lan2.put("name", "Swift");
 
            JSONObject lan3 = new JSONObject();
            lan3.put("id", 3);
            lan3.put("ide", "Visual Studio");
            lan3.put("name", "C#");
 
            JSONArray array = new JSONArray();
            array.put(lan1);
            array.put(lan2);
            array.put(lan3);
 
            root.put("languages", array);
 
            Log.d("info",root.toString());
 
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }
}

8+

留下评论