Android下Json文件解析

这篇文章为转载吧 看了两个大牛的博客
写博客有时也是给自己做个笔记吧
在很多时候都要用到Json解析
Json的定义:一种轻量级的数据交换格式,具有良好的可读和便于快速编写的特性。业内主流技术为其提供了完整的解决方案(有点类似于正则表达式 ,获得了当今大部分语言的支持),从而可以在不同平台间进行数据交换。JSON采用兼容性很高的文本格式,同时也具备类似于C语言体系的行为。 – Json.org
不多说废话了。。。
工程结构

关于读取assets和res下的文件 这篇博文写的很好~
Android中资源文件的使用

这个文章写Json写的也挺好的
Android下Json解析及简单应用

这是我自己写的一个小李子:

public class MainActivity extends Activity {
    private AssetManager assetManager;
    private TextView textView1;
    private TextView textView2;
    private TextView textView3;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        assetManager = getAssets();
        textView1 = (TextView)findViewById(R.id.textview1);
        textView2 = (TextView)findViewById(R.id.textview2);
        textView3 = (TextView)findViewById(R.id.textview3);
        try {
            InputStream inputStream = assetManager.open("data.json");
            byte[] buffer = new byte[inputStream.available()];
            inputStream.read(buffer);
            String jsonString = new String(buffer,"gbk");
            String path = "";
            String description ="";
            String name = "";
            try {
                JSONObject jsonObject = new JSONObject(jsonString);
                JSONArray jsonArray = jsonObject.getJSONArray("data");
                for(int i =0;i<jsonArray.length();i++){
                    JSONObject jsonObjectChild = ((JSONObject)jsonArray.opt(i));
                    path = path+jsonObjectChild.getString("path");
                    description = description +jsonObjectChild.getString("description");
                    name = name+jsonObjectChild.getString("name");
                }
            } catch (JSONException e) {
                // TODO 自动生成的 catch 块
                e.printStackTrace();
            }
            textView1.setText(path);
            textView2.setText(description);
            textView3.setText(name);
        } catch (IOException e) {
            // TODO 自动生成的 catch 块
            e.printStackTrace();
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}

Json文件 :
{“data”:[{“path”:”images\/pic0.jpg”,”description”:”0这是身啊加速度接啊快乐到家阿卡三等奖阿卡丽风景卡拉是否健康0”,”name”:”用户信息0”},{“path”:”images\/pic1.jpg”,”description”:”21这是身啊加速度接啊快乐到家阿卡三等奖阿卡丽风景卡拉是否健康1”,”name”:”用户信息1”},{“path”:”images\/pic2.jpg”,”description”:”42这是身啊加速度接啊快乐到家阿卡三等奖阿卡丽风景卡拉是否健康2”,”name”:”用户信息2”},{“path”:”images\/pic3.jpg”,”description”:”63这是身啊加速度接啊快乐到家阿卡三等奖阿卡丽风景卡拉是否健康3”,”name”:”用户信息3”},{“path”:”images\/pic4.jpg”,”description”:”84这是身啊加速度接啊快乐到家阿卡三等奖阿卡丽风景卡拉是否健康4”,”name”:”用户信息4”},{“path”:”images\/pic5.jpg”,”description”:”105这是身啊加速度接啊快乐到家阿卡三等奖阿卡丽风景卡拉是否健康5”,”name”:”用户信息5”},{“path”:”images\/pic6.jpg”,”description”:”126这是身啊加速度接啊快乐到家阿卡三等奖阿卡丽风景卡拉是否健康6”,”name”:”用户信息6”},{“path”:”images\/pic7.jpg”,”description”:”147这是身啊加速度接啊快乐到家阿卡三等奖阿卡丽风景卡拉是否健康7”,”name”:”用户信息7”},{“path”:”images\/pic8.jpg”,”description”:”168这是身啊加速度接啊快乐到家阿卡三等奖阿卡丽风景卡拉是否健康8”,”name”:”用户信息8”},{“path”:”images\/pic9.jpg”,”description”:”189这是身啊加速度接啊快乐到家阿卡三等奖阿卡丽风景卡拉是否健康9”,”name”:”用户信息9”}],”result”:”1”}

后面那个result是个string 直接getString(“result”);就可以了

效果图

结果

以上

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值