【求救!】从接口里获得的json字符串怎么截取?

狂我之心 2017-02-15 05:01:28
{"code":0,"message":"成功","data":[{"id":11035,"name":"13581555751","realName":null,"cateEnum":"USER","parentName":null,"venderId":null,"communityId":"null","super_":false}]}


就比如这个 我只想获取id值 需要怎么做呢?萌新求救
...全文
385 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
_周星星 2017-02-16
  • 打赏
  • 举报
回复
万一以后 你要取其它的属性了 。。。。。。。。。。。。。。
_周星星 2017-02-16
  • 打赏
  • 举报
回复
为毛 有这样的需求 把他转为实体 操作实体不好么
狂我之心 2017-02-16
  • 打赏
  • 举报
回复
引用 7 楼 zhumj_zhumj 的回复:
[quote=引用 6 楼 qq_36593018 的回复:] [quote=引用 5 楼 dalor 的回复:] [quote=引用 3 楼 qq_36593018 的回复:] public void onResponse(Call call, Response response) throws IOException { final String res = response.body().string(); L.e("onResponse:" + res); try { JSONArray jsonArray = new JSONArray(res); for (int i = 0; i <jsonArray.length() ; i++) { JSONObject jsonObject = jsonArray.getJSONObject(i); String id =jsonObject.getString("id"); L.e("+++++++++++++++++++"+id+"+++++++++++++++++++"); } } catch (JSONException e) { e.printStackTrace(); } } 我这样为什么不能得到id
返回的字符串是一个JSONObject,先转成jsonobject,再使用jsonobject对象根据key-->data,获取jsonarray.[/quote] ************************************************************************************************************************************* try { JSONObject j = new JSONObject(res); L.e("j:" + j); String str = "code=" + j.getString("code") + "message=" + j.getString("message") + "id="; L.e("str:" + str); JSONArray josnarray = new JSONArray("data"); L.e("josnarray:" + josnarray); for (int i = 0; i < josnarray.length(); i++) { JSONObject jsonObjectSon = (JSONObject) josnarray.opt(i); L.e("jsonObjectSon:" + jsonObjectSon); str = str + jsonObjectSon.getString("id") + "name:" + jsonObjectSon.getString("name"); } L.e(str); } catch (JSONException e) { e.printStackTrace(); } --------------------------------------------------------------------------------------------------------------------------------------------------------------- 02-16 01:14:24.247 2500-6699/www.csy.com.getnumber E/Imooc_okhttp: res:{"code":0,"message":"成功","data":[{"id":11035,"name":"13581555751","realName":null,"cateEnum":"USER","parentName":null,"venderId":null,"communityId":"null","super_":false}]} 02-16 01:14:24.247 2500-6699/www.csy.com.getnumber E/Imooc_okhttp: onResponse:Response{protocol=http/1.1, code=200, message=OK, url=http://117.121.38.80:8081/permission/mobileLogin?name=13581555751&password=123456} 02-16 01:14:24.247 2500-6699/www.csy.com.getnumber E/Imooc_okhttp: j:{"code":0,"message":"成功","data":[{"id":11035,"name":"13581555751","realName":null,"cateEnum":"USER","parentName":null,"venderId":null,"communityId":"null","super_":false}]} 02-16 01:14:24.247 2500-6699/www.csy.com.getnumber E/Imooc_okhttp: str:code=0message=成功id= 前面都能打印出来 就是到data里面的数据时为什么就打印不出来了呢/(ㄒoㄒ)/~~[/quote] 已经打印出来了,但是LOG有大小限制,所以没打印完全[/quote] 谢谢~~我已解决
头发还没秃a 2017-02-16
  • 打赏
  • 举报
回复
引用 6 楼 qq_36593018 的回复:
[quote=引用 5 楼 dalor 的回复:] [quote=引用 3 楼 qq_36593018 的回复:] public void onResponse(Call call, Response response) throws IOException { final String res = response.body().string(); L.e("onResponse:" + res); try { JSONArray jsonArray = new JSONArray(res); for (int i = 0; i <jsonArray.length() ; i++) { JSONObject jsonObject = jsonArray.getJSONObject(i); String id =jsonObject.getString("id"); L.e("+++++++++++++++++++"+id+"+++++++++++++++++++"); } } catch (JSONException e) { e.printStackTrace(); } } 我这样为什么不能得到id
返回的字符串是一个JSONObject,先转成jsonobject,再使用jsonobject对象根据key-->data,获取jsonarray.[/quote] ************************************************************************************************************************************* try { JSONObject j = new JSONObject(res); L.e("j:" + j); String str = "code=" + j.getString("code") + "message=" + j.getString("message") + "id="; L.e("str:" + str); JSONArray josnarray = new JSONArray("data"); L.e("josnarray:" + josnarray); for (int i = 0; i < josnarray.length(); i++) { JSONObject jsonObjectSon = (JSONObject) josnarray.opt(i); L.e("jsonObjectSon:" + jsonObjectSon); str = str + jsonObjectSon.getString("id") + "name:" + jsonObjectSon.getString("name"); } L.e(str); } catch (JSONException e) { e.printStackTrace(); } --------------------------------------------------------------------------------------------------------------------------------------------------------------- 02-16 01:14:24.247 2500-6699/www.csy.com.getnumber E/Imooc_okhttp: res:{"code":0,"message":"成功","data":[{"id":11035,"name":"13581555751","realName":null,"cateEnum":"USER","parentName":null,"venderId":null,"communityId":"null","super_":false}]} 02-16 01:14:24.247 2500-6699/www.csy.com.getnumber E/Imooc_okhttp: onResponse:Response{protocol=http/1.1, code=200, message=OK, url=http://117.121.38.80:8081/permission/mobileLogin?name=13581555751&password=123456} 02-16 01:14:24.247 2500-6699/www.csy.com.getnumber E/Imooc_okhttp: j:{"code":0,"message":"成功","data":[{"id":11035,"name":"13581555751","realName":null,"cateEnum":"USER","parentName":null,"venderId":null,"communityId":"null","super_":false}]} 02-16 01:14:24.247 2500-6699/www.csy.com.getnumber E/Imooc_okhttp: str:code=0message=成功id= 前面都能打印出来 就是到data里面的数据时为什么就打印不出来了呢/(ㄒoㄒ)/~~[/quote] 已经打印出来了,但是LOG有大小限制,所以没打印完全
狂我之心 2017-02-16
  • 打赏
  • 举报
回复
引用 5 楼 dalor 的回复:
[quote=引用 3 楼 qq_36593018 的回复:] public void onResponse(Call call, Response response) throws IOException { final String res = response.body().string(); L.e("onResponse:" + res); try { JSONArray jsonArray = new JSONArray(res); for (int i = 0; i <jsonArray.length() ; i++) { JSONObject jsonObject = jsonArray.getJSONObject(i); String id =jsonObject.getString("id"); L.e("+++++++++++++++++++"+id+"+++++++++++++++++++"); } } catch (JSONException e) { e.printStackTrace(); } } 我这样为什么不能得到id
返回的字符串是一个JSONObject,先转成jsonobject,再使用jsonobject对象根据key-->data,获取jsonarray.[/quote] ************************************************************************************************************************************* try { JSONObject j = new JSONObject(res); L.e("j:" + j); String str = "code=" + j.getString("code") + "message=" + j.getString("message") + "id="; L.e("str:" + str); JSONArray josnarray = new JSONArray("data"); L.e("josnarray:" + josnarray); for (int i = 0; i < josnarray.length(); i++) { JSONObject jsonObjectSon = (JSONObject) josnarray.opt(i); L.e("jsonObjectSon:" + jsonObjectSon); str = str + jsonObjectSon.getString("id") + "name:" + jsonObjectSon.getString("name"); } L.e(str); } catch (JSONException e) { e.printStackTrace(); } --------------------------------------------------------------------------------------------------------------------------------------------------------------- 02-16 01:14:24.247 2500-6699/www.csy.com.getnumber E/Imooc_okhttp: res:{"code":0,"message":"成功","data":[{"id":11035,"name":"13581555751","realName":null,"cateEnum":"USER","parentName":null,"venderId":null,"communityId":"null","super_":false}]} 02-16 01:14:24.247 2500-6699/www.csy.com.getnumber E/Imooc_okhttp: onResponse:Response{protocol=http/1.1, code=200, message=OK, url=http://117.121.38.80:8081/permission/mobileLogin?name=13581555751&password=123456} 02-16 01:14:24.247 2500-6699/www.csy.com.getnumber E/Imooc_okhttp: j:{"code":0,"message":"成功","data":[{"id":11035,"name":"13581555751","realName":null,"cateEnum":"USER","parentName":null,"venderId":null,"communityId":"null","super_":false}]} 02-16 01:14:24.247 2500-6699/www.csy.com.getnumber E/Imooc_okhttp: str:code=0message=成功id= 前面都能打印出来 就是到data里面的数据时为什么就打印不出来了呢/(ㄒoㄒ)/~~
开发者_android 2017-02-15
  • 打赏
  • 举报
回复
引用 3 楼 qq_36593018 的回复:
public void onResponse(Call call, Response response) throws IOException { final String res = response.body().string(); L.e("onResponse:" + res); try { JSONArray jsonArray = new JSONArray(res); for (int i = 0; i <jsonArray.length() ; i++) { JSONObject jsonObject = jsonArray.getJSONObject(i); String id =jsonObject.getString("id"); L.e("+++++++++++++++++++"+id+"+++++++++++++++++++"); } } catch (JSONException e) { e.printStackTrace(); } } 我这样为什么不能得到id
返回的字符串是一个JSONObject,先转成jsonobject,再使用jsonobject对象根据key-->data,获取jsonarray.
狂我之心 2017-02-15
  • 打赏
  • 举报
回复
引用 2 楼 dalor 的回复:
整个先转化成json对象,再获取data数组,再遍历数组,转化成JSON对象,根据key,也就是‘id’,获取值。 API:JSONObject JSONArray
三楼是我回复你的
狂我之心 2017-02-15
  • 打赏
  • 举报
回复
public void onResponse(Call call, Response response) throws IOException { final String res = response.body().string(); L.e("onResponse:" + res); try { JSONArray jsonArray = new JSONArray(res); for (int i = 0; i <jsonArray.length() ; i++) { JSONObject jsonObject = jsonArray.getJSONObject(i); String id =jsonObject.getString("id"); L.e("+++++++++++++++++++"+id+"+++++++++++++++++++"); } } catch (JSONException e) { e.printStackTrace(); } } 我这样为什么不能得到id
开发者_android 2017-02-15
  • 打赏
  • 举报
回复
整个先转化成json对象,再获取data数组,再遍历数组,转化成JSON对象,根据key,也就是‘id’,获取值。 API:JSONObject JSONArray
狂我之心 2017-02-15
  • 打赏
  • 举报
回复
顺便一提 这个数据是访问一个接口,从接口里获得的数据

80,337

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧