Android之解析Json数据 .
来源:互联网 发布:linux usr目录 编辑:程序博客网 时间:2024/05/17 08:01
在Android中被广泛运用于客户端和网络(或者说服务器)通信。
JSON 表示名称 / 值对的方式
按照最简单的形式,可以用下面这样的 JSON 表示"名称 / 值对":
{ "name": "Brett", "lage":22,"sex": "女" } ,这表示了一个JsonObject。
[{name:"张三:",age:21,sex:"女"},{name:"李斯",age:21,sex:"女"},{name:"王五",age:21,sex:"女"}],使用中括弧表示JsonArray,是json对象数组。
一、解析第一种单个json对象的json数据。数据从网络上获取。演示实例为 查询手机号码归属地。
- URL url;
- StringBuffer sb = new StringBuffer();
- String line = null;
- try {
- url = new URL(
- "http://api.showji.com/Locating/default.aspx?m=13763089126&output=json&callback=querycallback");
- HttpURLConnection conn = (HttpURLConnection) url.openConnection();
- InputStream is = conn.getInputStream();
- BufferedReader buffer = new BufferedReader(
- new InputStreamReader(is));
- while ((line = buffer.readLine()) != null) {
- sb.append(line);
- }
- } catch (MalformedURLException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- }
URL url;StringBuffer sb = new StringBuffer();String line = null;try {url = new URL("http://api.showji.com/Locating/default.aspx?m=13763089126&output=json&callback=querycallback");HttpURLConnection conn = (HttpURLConnection) url.openConnection();InputStream is = conn.getInputStream();BufferedReader buffer = new BufferedReader(new InputStreamReader(is));while ((line = buffer.readLine()) != null) {sb.append(line);}} catch (MalformedURLException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}
此处获取的数据为:
querycallback({"Mobile":"13763******","QueryResult":"True","Province":"广东","City":"湛江","AreaCode":"0759","PostCode":"524000","Corp":"中国移动","Card":"GSM"});
需要截取这个json对象出来。
String js = sb.substring(sb.indexOf("{"), sb.indexOf("}") + 1);
下面函数解析json对象,返回一个Callerloc对象
Callerloc是一个实体类
- private Callerloc parse(String json) {
- Callerloc my = null;
- if (json == null || json.length() < 1)
- return null;
- try {
- my = new Callerloc();
- JSONObject jsonobj = new JSONObject(json);
private Callerloc parse(String json) {Callerloc my = null;if (json == null || json.length() < 1)return null;try {my = new Callerloc();JSONObject jsonobj = new JSONObject(json);
- my.setMobile(jsonobj.getString("Mobile"));
- my.setQueryResult(jsonobj.getString("QueryResult"));
- my.setProvince(jsonobj.getString("Province"));
- my.setCity(jsonobj.getString("City"));
- my.setAreaCode(jsonobj.getString("AreaCode"));
- my.setPostCode(jsonobj.getString("PostCode"));
- my.setCard(jsonobj.getString("Card"));
- my.setCorp(jsonobj.getString("Corp"));
my.setMobile(jsonobj.getString("Mobile"));my.setQueryResult(jsonobj.getString("QueryResult"));my.setProvince(jsonobj.getString("Province"));my.setCity(jsonobj.getString("City"));my.setAreaCode(jsonobj.getString("AreaCode"));my.setPostCode(jsonobj.getString("PostCode"));my.setCard(jsonobj.getString("Card"));my.setCorp(jsonobj.getString("Corp"));
- } catch (JSONException e) {
- e.printStackTrace();
- }
- return my;
- }
} catch (JSONException e) {e.printStackTrace();}return my;}
二、解析json数组
json数据为:[{name:"张三:",age:21,sex:"女"},{name:"李斯",age:21,sex:"女"},{name:"王五",age:21,sex:"女"}]
返回list
- private ArrayList<myjson> parsem(String json) {
- myjson my = null;
- if (json == null || json.length() < 1)
- return null;
- try {
- JSONArray jsonary = new JSONArray(json);
- ArrayList<myjson> objlist = new ArrayList<myjson>();
- for (int i = 0; i < jsonary.length(); i++) {
- my = new myjson();
- JSONObject jsonobj = jsonary.getJSONObject(i);
- my.set_name(jsonobj.getString("name"));
- my.set_age(jsonobj.getInt("age"));
- my.set_sex(jsonobj.getString("sex"));
- objlist.add(my);
- }
- return objlist;
- } catch (JSONException e) {
- e.printStackTrace();
- }
- return null;
- }
- Android之解析Json数据
- Android之json数据解析
- Android之json数据解析
- Android之解析Json数据 .
- Android之解析json数据
- Android之json数据解析
- Android之json数据解析
- Android之json数据解析
- Android之json数据解析
- Android之解析json数据
- Android 数据解析之 Json
- android 之 json数据解析
- Android 数据之Json解析
- Android数据解析之JSON
- Android数据解析之JSON
- Android数据之Json解析
- Android之解析json数据
- android数据解析之Json
- 一个程序员多年的收藏
- Android系统手机端抓包方法
- Android 向多媒体数据库增加音频文件
- JavaScript操作数据库总结
- 设计模式原来如此-策略模式(Strategy Pattern)
- Android之解析Json数据 .
- ORCLE lightweight jobs
- android java.net.UnknownHostException: Unable to resolve host "...": No address associated 错误
- C# Office组件 dll word.dll excel.dll
- 百度地图API之ItemizedOverlay的使用(Android)
- win8开发 常用的布局控件
- XP无法访问Win7文件解决方法
- 第51天的交易(2013-6-7)(-100)(-250) (又是一个失望的非农夜)
- 当前打开命令cmd