JSON解析

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

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

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

import com.example.day14json.bean.People;

public class MainActivity extends Activity {

    List<People> list;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        String jsonStr = getJsonFromAssets("json.txt");
        Log.i("================", jsonStr);
        
        list = new ArrayList<People>();
        
        //使用读取出来的json字符串进行  json解析
        parseJson(jsonStr);
        
        Log.i("=======================", list.toString());
    }

    private void parseJson(String jsonStr) {
        try {
            JSONObject object = new JSONObject(jsonStr);
            JSONArray peopleArray = object.optJSONArray("people");
            for (int i = 0; i < peopleArray.length(); i++) {
                //从数组中过去数据 使用的方法 要根据数组中的数据格式来确定
                //如果数组中是Json对象结构 使用optJSONObject(index)
                //如果数组中是String结构 使用optString(index)
                JSONObject peopleObject = peopleArray.optJSONObject(i);
                String email = peopleObject.optString("email");
                String firstName = peopleObject.optString("firstName");
                String lastName = peopleObject.optString("lastName");
                
                People people = new People(email, firstName, lastName);
                list.add(people);

            }
        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    private String getJsonFromAssets(String fileName) {
        String jsonStr = "";
        //从assets文件夹中   把json.txt中的json字符串读取到内存中
        try {
            InputStream is = getAssets().open(fileName);
            //把输入流中的内容读到内存中
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            //如果数据输出到了byte数组中  我们可以使用new String("byte数组")转成字符串
            //把数据流中的数据  放入到 输出流中
            int len = -1;
            byte[] buff = new byte[1024];
            while((len = is.read(buff)) != -1){
                bos.write(buff, 0, len);
            }
            //当while循环结束时 所有的数据就写到了输出流中
            //使用字节数组数组输出流得到一个  字节数组
            byte[] byteArray = bos.toByteArray();
            jsonStr = new String(byteArray);
            
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return jsonStr;
    }
    
    

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值