• Adroid解析json


    参考资料:

    http://www.open-open.com/lib/view/open1326376799874.html

    http://www.cnblogs.com/tt_mc/archive/2011/01/04/1925327.html

    总结:

    解析普通json:使用JSONObject,getString方法;

    解析数组形式json:使用JSONArray,getJSONArray方法后,遍历数组,使用普通json的解析方法;

    public class GetPostMain extends Activity
    {
        Button get , post;
        EditText show;
        @Override
        public void onCreate(Bundle savedInstanceState)
        {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
            get = (Button) findViewById(R.id.get);
            post = (Button) findViewById(R.id.post);
            show = (EditText)findViewById(R.id.show);
            get.setOnClickListener(new OnClickListener()
            {
                @Override
                public void onClick(View v)
                {
                    String response = GetPostUtil
                        .sendGet("http://www.weather.com.cn/data/sk/101281601.html" , null);
                    show.setText(response);
                    
                }
            });
            post.setOnClickListener(new OnClickListener()
            {
                @Override
                public void onClick(View v)
                {
                    String data = "";
                    try{
                        // 构造json
                        JSONObject param = new JSONObject();  
                        param.put("userid", "uid45798");  
                        param.put("lat", "32.038587000000");
                        param.put("lon", "118.778906000000");
                        data = param.toString();  
                    }catch(JSONException e){
                        Log.e("postdata", "Json post error");
                    }
                    String response = GetPostUtil.sendPost("http://xxxx.getNearbyActivity", data);
                    Log.d("postdata",data);

    String str1
    = ""; String str2 = ""; try{ // 获取单个key值 JSONObject jsonObj = new JSONObject(response.toString()); String errorcode = "errorcode:" + jsonObj.getString("errorcode"); String count = "count:" + jsonObj.getString("count"); String userid = "userid:" + jsonObj.getString("userid"); str1 = errorcode+" "+userid+" "+count+" "; }catch(JSONException e){ System.out.println("Json parse error"); str1 = "str1没有数据 "; } try{ // 获取多组json JSONArray jsonObjs = new JSONObject(response.toString()).getJSONArray("datalist"); for(int i = 0; i < jsonObjs.length() ; i++){ JSONObject jsonObj = (JSONObject)jsonObjs.opt(i); String brand = "brand:"+ jsonObj.getString("brand"); String aid = "aid:"+ jsonObj.getString("aid"); str2 += aid+","+brand+" "; } }catch(JSONException e){ System.out.println("Json parse error"); str2 = "str2没有数据"; } show.setText(str1+str2); } }); } }
  • 相关阅读:
    基本几何变换——以鼠标为中心进行缩放
    jquery radio/checkbox change 事件不能触发的问题
    CCF YOCSEF“面向科学的大数据管理与分析”报告会即将举行
    mac iTunes启动失败,声称iTunes文件夹被锁定
    opencv2.3 + visual studio 2010 编译配置方法
    Qt QGLWidget 不能够实时刷新的问题
    在mac os x中建立事件钩子
    二叉树前序、中序、后序、层次遍历
    CRC从原理到实现
    异步FIFO的FPGA实现
  • 原文地址:https://www.cnblogs.com/sudawei/p/3383893.html
Copyright © 2020-2023  润新知