zoukankan      html  css  js  c++  java
  • android json 解析简单实例

    Android JSON解析跟JAVA 的JSON解析原理是一样的. Android自带的JSON方式跟方便,不需要导包啥的.不深究原理了,直接上代码:
    public class JsonActivity extends Activity {     
        private TextView tvJson;  
        private Button btnJson;  
        private Button btnJsonMulti;  
        @Override  
        public void onCreate(Bundle savedInstanceState) {  
            super.onCreate(savedInstanceState);  
            setContentView(R.layout.main);  
            tvJson = (TextView) this.findViewById(R.id.tvJson);  
            btnJson = (Button) this.findViewById(R.id.btnJson);  
            btnJsonMulti = (Button) this.findViewById(R.id.btnJsonMulti);  
            btnJson.setOnClickListener(new View.OnClickListener() {  
                @Override  
                public void onClick(View v) {  
                    String strUrl = ServerPageUtil.getStrUrl(UrlsOfServer.JSON_SINGER);  
                    //获得返回的Json字符串  
                    String strResult = connServerForResult(strUrl);  
                    //解析Json字符串  
                    parseJson(strResult);  
                }  
            });  
            btnJsonMulti.setOnClickListener(new View.OnClickListener() {  
                @Override  
                public void onClick(View v) {  
                    String strUrl = ServerPageUtil.getStrUrl(UrlsOfServer.JSON_SINGERS);  
                    String strResult = connServerForResult(strUrl);  
                    //获得多个Singer  
                    parseJsonMulti(strResult);  
                }  
            });  
        }  
        private String connServerForResult(String strUrl) {  
            // HttpGet对象  
            HttpGet httpRequest = new HttpGet(strUrl);  
            String strResult = "";  
            try {  
                // HttpClient对象  
                HttpClient httpClient = new DefaultHttpClient();  
                // 获得HttpResponse对象  
                HttpResponse httpResponse = httpClient.execute(httpRequest);  
                if (httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {  
                    // 取得返回的数据  
                    strResult = EntityUtils.toString(httpResponse.getEntity());  
                }  
            } catch (ClientProtocolException e) {  
                tvJson.setText("protocol error");  
                e.printStackTrace();  
            } catch (IOException e) {  
                tvJson.setText("IO error");  
                e.printStackTrace();  
            }  
            return strResult;  
        }  
        // 解析单个Json数据  
        private void parseJson(String strResult) {  
            try {  
                JSONObject jsonObj = new JSONObject(strResult).getJSONObject("singer");  
                int id = jsonObj.getInt("id");  
                String name = jsonObj.getString("name");  
                String gender = jsonObj.getString("gender");  
                tvJson.setText("ID号"+id + ", 姓名:" + name + ",性别:" + gender);  
            } catch (JSONException e) {  
                System.out.println("Json parse error");  
                e.printStackTrace();  
            }  
        }  
      //解析多个Json的数据  
        private void parseJsonMulti(String strResult) {  
            try {  
                JSONArray jsonObjs = new JSONObject(strResult).getJSONArray("singers");  
                String s = "";  
                for(int i = 0; i < jsonObjs.length() ; i++){  
                    JSONObject jsonObj = ((JSONObject)jsonObjs.opt(i))  
                    .getJSONObject("singer");  
                    int id = jsonObj.getInt("id");  
                    String name = jsonObj.getString("name");  
                    String gender = jsonObj.getString("gender");  
                    s += "ID号"+id + ", 姓名:" + name + ",性别:" + gender+ "
    " ;  
                }  
                tvJson.setText(s);  
            } catch (JSONException e) {  
                System.out.println("Jsons parse error !");  
                e.printStackTrace();  
            }
        }  
     }  

  • 相关阅读:
    gateway调用Fegin失败问题解决
    JVM调试命令简介
    oracle10g登录em后,提示“java.lang.Exception: Exception in sending Request :: null”
    vs2019中让ashx 文件中折叠收起代码
    oracle多表视图不自动更新,手动刷新视图获得基本表的数据
    可以对表单中的隐藏字段进行操纵 问题参数 __VIEWSTATEGENERATOR
    SQL Server 检测到基于一致性的逻辑 I/O 错误 页撕裂
    WIN2016和WIN10关闭同步主机服务,节省磁盘频繁读取,并关闭自动维护
    IIS网站应用程序配置不继承网站.net框架版本
    c#web错误码CS0227,不安全代码只会在使用/unsafe编译情况下出现
  • 原文地址:https://www.cnblogs.com/aikongmeng/p/3697352.html
Copyright © 2011-2022 走看看