android studio jason文件解析基础

来源:互联网 发布:淘宝拉帮结派流量协议 编辑:程序博客网 时间:2024/05/16 17:15
解析jason中非数组元素:
tv=(TextView)findViewById(R.id.text);String jsonstr="{\"name\":\"ailsa\",\"age\":22,\"arr\":[1,2,\"hello\"]}";try {    JSONObject jsonObject=new JSONObject(jsonstr);    String name=jsonObject.getString("name");    tv.setText(name);} catch (JSONException e) {    e.printStackTrace();}
解析jason中数组元素:(如何这里不能正常输出,可以尝试重启studio)
tv=(TextView)findViewById(R.id.text);String jsonstr="{\"name\":\"ailsa\",\"age\":22,\"arr\":[1,2,\"hello\"]}";try {    JSONObject jsonObject=new JSONObject(jsonstr);    JSONArray arr=jsonObject.getJSONArray("arr");    int value=arr.getInt(0);    System.out.println(value);} catch (JSONException e) {    e.printStackTrace();}

0 0
原创粉丝点击