package com.example.jsonresolvetest;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.List;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.example.jsonresolvetest.bean.ArenaBean;

import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.os.Build;

public class MainActivity extends ActionBarActivity {
    
    //这个类就是解析类
 //1 从网络拿,展示数据,2从本地json里拿,展示数据
    private ListView lv_arena;
    

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        lv_arena = (ListView) findViewById(R.id.lv_arena);
        
        jsonData(MainActivity.this, "jsonarena.json");/**尤其需要注意:这里的json文件要带扩展名,要写成"jsonarena.json" 而不是"jsonarena"*/
    }
    
     public static JSONObject jsonData(Context context,String fileName){//filename assets目录下的json文件名  
            JSONObject jsonDate=null; 
            
            try {  
         //jsonarena.json文件是放在项目的assets文件夹下的 InputStreamReader inputStreamReader
=new InputStreamReader(context.getAssets().open(fileName),"UTF-8"); //找不着jsonarena文件 BufferedReader bufferedReader=new BufferedReader(inputStreamReader); String line; StringBuilder stringBuilder=new StringBuilder(); while ((line=bufferedReader.readLine())!=null){ stringBuilder.append(line); } bufferedReader.close(); inputStreamReader.close(); jsonDate=JSON.parseObject(stringBuilder.toString());//得到JSONobject对象 Log.e("jsonDate","=="+jsonDate); int retArray=jsonDate.getIntValue("ret");//得到ret的内容 Log.e("retArray","=="+retArray); String msgArray = jsonDate.getString("msg");//得到msg的内容 Log.e("msgArray","=="+msgArray); JSONArray arenaArray=jsonDate.getJSONArray("arena");//得到arena节点,不懂得看Log打印就清楚了 Log.e("arenaArray","=="+arenaArray); List<ArenaBean> listArenaBean=JSON.parseArray(arenaArray.toString(),ArenaBean.class); //Series 相当于ArenaBean节点 for(ArenaBean arenaBean:listArenaBean){ Log.e("arena.getId","="+arenaBean.getId()); Log.e("arena.getname","="+arenaBean.getName()); Log.e("arena.getId","="+arenaBean.getStatus()); } } catch (IOException e) { e.printStackTrace(); } return jsonDate; } }
package com.example.jsonresolvetest.bean;

public class ArenaBean {

    private int id;
    private String name;
    private int status;
    
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getStatus() {
        return status;
    }
    public void setStatus(int status) {
        this.status = status;
    }
    
    
    
}