zoukankan      html  css  js  c++  java
  • [入门阅读]怎样在android中解析JSON

    JSON入门介绍:http://kirin.javaeye.com/blog/616226

    也参考了此篇:http://blog.163.com/fushaolin@126/blog/static/16341724220108244251686/

    json数据格式解析我自己分为两种:一种是普通的,一种是带有数组形式的:http://archive.cnblogs.com/a/1925327/

    先实例化个JSONObject对象

    [java] view plaincopyprint?
    
        JSONObject aJosnObj = new JSONObject(jsonStr);//jsonStr为对应json字符串数据 

    然后再根据json数据的实际情况调用有关方法。

    这是一个利用JSON定位的例子:

     1 /**  
     2   * Google定位的实现.<br/>  
     3   * Geolocation的详细信息请参见:<br/>  
     4   * <a  
     5   * href="http://code.google.com/apis/gears/geolocation_network_protocol.html" mce_href="http://code.google.com/apis/gears/geolocation_network_protocol.html">  
     6   * http://code.google.com/apis/gears/geolocation_network_protocol.html</a>  
     7   */    
     8 public class LocationAct extends Activity {    
     9      private TextView txtInfo;    
    10      public void onCreate(Bundle savedInstanceState) {    
    11          super.onCreate(savedInstanceState);    
    12          setContentView(R.layout.main);    
    13          Button btn = (Button) findViewById(R.id.btnStart);    
    14          txtInfo = (TextView) findViewById(R.id.txtInfo);    
    15          btn.setOnClickListener(new Button.OnClickListener() {    
    16              public void onClick(View view) {    
    17                  getLocation();    
    18              }    
    19          });    
    20      }    
    21      private void getLocation() {    
    22          TelephonyManager tm = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);    
    23          GsmCellLocation gsmCell = (GsmCellLocation) tm.getCellLocation();    
    24          int cid = gsmCell.getCid();    
    25          int lac = gsmCell.getLac();    
    26          String netOperator = tm.getNetworkOperator();    
    27          int mcc = Integer.valueOf(netOperator.substring(0, 3));    
    28          int mnc = Integer.valueOf(netOperator.substring(3, 5));    
    29          JSONObject holder = new JSONObject();    
    30          JSONArray array = new JSONArray();    
    31          JSONObject data = new JSONObject();    
    32          try {    
    33              holder.put("version", "1.1.0");    
    34              holder.put("host", "maps.google.com");    
    35              holder.put("address_language", "zh_CN");    
    36              holder.put("request_address", true);    
    37              holder.put("radio_type", "gsm");    
    38              holder.put("carrier", "HTC");    
    39              data.put("cell_id", cid);    
    40              data.put("location_area_code", lac);    
    41              data.put("mobile_countyr_code", mcc);    
    42              data.put("mobile_network_code", mnc);    
    43              array.put(data);    
    44              holder.put("cell_towers", array);    
    45          } catch (JSONException e) {    
    46              e.printStackTrace();    
    47          }    
    48          DefaultHttpClient client = new DefaultHttpClient();    
    49          HttpPost httpPost = new HttpPost("http://www.google.com/loc/json");    
    50          StringEntity stringEntity = null;    
    51          try {    
    52              stringEntity = new StringEntity(holder.toString());    
    53          } catch (UnsupportedEncodingException e) {    
    54              e.printStackTrace();    
    55          }    
    56          httpPost.setEntity(stringEntity);    
    57          HttpResponse httpResponse = null;    
    58          try {    
    59              httpResponse = client.execute(httpPost);    
    60          } catch (ClientProtocolException e) {    
    61              e.printStackTrace();    
    62          } catch (IOException e) {    
    63              e.printStackTrace();    
    64          }    
    65          HttpEntity httpEntity = httpResponse.getEntity();    
    66          InputStream is = null;    
    67          try {    
    68              is = httpEntity.getContent();    
    69          } catch (IllegalStateException e) {    
    70              e.printStackTrace();    
    71          } catch (IOException e) {    
    72              // TODO Auto-generated catch block    
    73              e.printStackTrace();    
    74          }    
    75          InputStreamReader isr = new InputStreamReader(is);    
    76          BufferedReader reader = new BufferedReader(isr);    
    77          StringBuffer stringBuffer = new StringBuffer();    
    78          try {    
    79              String result = "";    
    80              while ((result = reader.readLine()) != null) {    
    81                  stringBuffer.append(result);    
    82              }    
    83          } catch (IOException e) {    
    84              e.printStackTrace();    
    85          }    
    86          txtInfo.setText(stringBuffer.toString());    
    87      }    
    88 } 
  • 相关阅读:
    镜像源收集
    关于vue-cli3脚手架安装后回退到vue-cli2版本的问题
    window.location 对象
    常用正则表达式
    前端开发工程师面试题
    面试题1
    Echarts.js使用
    swipe.js 使用方法
    canvas基础API
    前端面试题集锦
  • 原文地址:https://www.cnblogs.com/lxshanye/p/3490411.html
Copyright © 2011-2022 走看看