Android中JSON数据格式的简单使用

源码:

package com.wangzhu.demo;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;

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

import android.content.Context;

/**
 * JSON:JavaScript对象表示法(JavaScript Object Notation)。 <br/>
 * JSON是存储和交换文本信息的语法。<br/>
 * 
 * 特点:<br/>
 * JSON是轻量级的文本数据交换格式<br/>
 * JSON独立于语言和平台<br/>
 * JSON具有自我描述性,更以理解<br/>
 * 
 * 与XML的区别:<br/>
 * 类似XML,比XML更小、更快、更易解析<br/>
 * 没有结束标签<br/>
 * 更短<br/>
 * 读写的速度更快<br/>
 * 使用数组<br/>
 * 不使用保留字<br/>
 * 
 * JSON语法是JavaScript对象表示法语法的子集。<br/>
 * 数据在名称/值对中<br/>
 * 数据由逗号分隔<br/>
 * 花括号保存对象<br/>
 * 方括号保存数组<br/>
 * 
 * JSON值可以是:<br/>
 * 数字(正数或浮点数)<br/>
 * 字符串(在双引号中)<br/>
 * 逻辑值(true或false)<br/>
 * 数组(在方括号中)<br/>
 * 对象(在花括号中)<br/>
 * null<br/>
 * 
 * @author wangzhu
 * @date 2014年11月15日 下午11:37:38
 */
public class JSONUtils {

    private Context context;

    public JSONUtils(Context context) {
        this.context = context;
    }

    /**
     * 读取Json对象
     */
    public void readJsonObject() {
        String json = readFile();
        try {
            JSONObject root = new JSONObject(json);
            System.err.println("cat=" + root.getString("cat"));
            JSONArray array = root.getJSONArray("language");
            for (int i = 0; i < array.length(); i++) {
                System.err.println("------------");
                JSONObject object = array.getJSONObject(i);
                System.err.println("id=" + object.getInt("id"));
                System.err.println("ide=" + object.getString("ide"));
                System.err.println("name=" + object.getString("name"));
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    /**
     * 读取文件中的内容,文本编码方式为UTF-8
     * 
     * @return
     */
    private String readFile() {
        StringBuilder accum = new StringBuilder();
        InputStreamReader isr = null;
        BufferedReader br = null;
        try {
            isr = new InputStreamReader(context.getAssets().open(
                    "testJson.json"), "UTF-8");
            br = new BufferedReader(isr);
            String line = null;
            while ((line = br.readLine()) != null) {
                accum.append(line);
            }

        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (br != null) {
                try {
                    br.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (isr != null) {
                try {
                    isr.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return accum.toString();
    }

    /**
     * 创建Json对象
     */
    public void createJsonObject() {
        try {
            JSONObject root = new JSONObject();
            root.put("cat", "it");
            JSONArray array = new JSONArray();

            JSONObject lan1 = new JSONObject();
            lan1.put("id", 1);
            lan1.put("ide", "Eclipse");
            lan1.put("name", "Java");
            array.put(lan1);

            JSONObject lan2 = new JSONObject();
            lan2.put("id", 2);
            lan2.put("ide", "Xcode");
            lan2.put("name", "Swift");
            array.put(lan2);

            JSONObject lan3 = new JSONObject();
            lan3.put("id", 3);
            lan3.put("ide", "Visual Studio");
            lan3.put("name", "C#");
            array.put(lan3);
            root.put("language", array);

            System.err.println("createJsonObject: " + root.toString());

        } catch (JSONException e) {
            e.printStackTrace();
        }
    }
} 

截图:

Json文件:

执行结果:

2、创建Json对象

/**
     * 创建Json对象
     */
    private void createJsonObject() {
        try {
            JSONObject root = new JSONObject();
            root.put("cat", "it");
            JSONArray array = new JSONArray();

            JSONObject lan1 = new JSONObject();
            lan1.put("id", 1);
            lan1.put("ide", "Eclipse");
            lan1.put("name", "Java");
            array.put(lan1);

            JSONObject lan2 = new JSONObject();
            lan2.put("id", 2);
            lan2.put("ide", "Xcode");
            lan2.put("name", "Swift");
            array.put(lan2);

            JSONObject lan3 = new JSONObject();
            lan3.put("id", 3);
            lan3.put("ide", "Visual Studio");
            lan3.put("name", "C#");
            array.put(lan3);
            root.put("language", array);

            System.err.println("createJsonObject: " + root.toString());

        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

执行结果:

原文地址:https://www.cnblogs.com/xiaoxian1369/p/4100801.html