创建并读取JSON格式数据

  1. 创建JSON格式的数据
package com.example.testjson;


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

import android.os.Bundle;
import android.app.Activity;


public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        try {
            JSONObject lan1 = new JSONObject();
            JSONObject lan2 = new JSONObject();
            JSONObject lan3 = new JSONObject();

            lan1.put("firstName", "John");
            lan1.put("lastName", "Doe");
            lan2.put("firstName", "Anna");
            lan2.put("lastName", "Smith");
            lan3.put("firstName", "Peter");
            lan3.put("lastName", "Jones");

            JSONArray array = new JSONArray();
            array.put(lan1);
            array.put(lan2);
            array.put(lan3);

            JSONObject root = new JSONObject();
            root.put("employees", array);
            System.out.println(root.toString());

        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}
  1. 读取JSON格式数据:
    JSON文件:test.json,并把它放在eclipse android的assets目录下
    {
    “employees”:
    [
    { “firstName”:”John” , “lastName”:”Doe” },
    { “firstName”:”Anna” , “lastName”:”Smith” },
    { “firstName”:”Peter” , “lastName”:”Jones” }
    ]
    }
package com.example.testjson;

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.os.Bundle;
import android.app.Activity;
import android.view.Menu;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        try {
            //InputStreamReader 是字节流通向字符流的桥梁:它使用指定的 charset 读取字节并将其解码为字符
            InputStreamReader isr = new InputStreamReader(getAssets().open("test.json"),"UTF-8");
            BufferedReader br = new BufferedReader(isr);

            String line;
            StringBuilder builder = new StringBuilder();

            //br.readLine()表示读取一个文本行
            while((line = br.readLine()) != null){
                builder.append(line);
            }

            br.close();
            isr.close();

            JSONObject root = new JSONObject(builder.toString());
            JSONArray array = root.getJSONArray("employees");
            for(int i = 0;i < array.length();i++){
                JSONObject lan = array.getJSONObject(i);
                System.out.println(i);
                System.out.println(lan.getString("firstName") + "----" + lan.getString("lastName"));
            }

        } catch (UnsupportedEncodingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值