您的位置:首页 > 移动开发 > Android开发

Android下Json文件解析

2015-09-05 12:30 477 查看
这篇文章为转载吧 看了两个大牛的博客

写博客有时也是给自己做个笔记吧

在很多时候都要用到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”);就可以了

效果图



以上
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  android json