zoukankan      html  css  js  c++  java
  • 第十七章:android解析JSON

    一、解析JSON数据:

    首先引入包import org.json.JSONObject;(android sdk 14以后应该自带了 )

    Android端的程序解析JSON和JSON数组:

    package com.example.helloandroid;
    
    import org.json.JSONArray;
    import org.json.JSONException;
    import org.json.JSONObject;
    
    import android.app.Activity;
    import android.os.Bundle;
    import android.view.Menu;
    import android.view.MenuItem;
    import android.widget.Toast;
    
    public class JSONActivity extends Activity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_json);
            //JSON 解析
            String strJson = "{"sid":"2","name":"张三"}";
            try {
                JSONObject userObject = new JSONObject(strJson);
                String name = userObject.getString("name");
                Toast.makeText(JSONActivity.this, name, 1000).show();
            } catch (JSONException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } 
            //JSON 数组解析
            String strArrayJson = "[{"sid":"1","name":"张三"},{"sid":"2","name":"张四"}]";
            JSONArray userJsonArray;
            try {
                userJsonArray = new JSONArray(strArrayJson );
                for(int i=0;i<userJsonArray.length();++i){
    
                    JSONObject userJObject = (JSONObject) userJsonArray.get(i);
    
                    String name = userJObject.getString("name");
                    Toast.makeText(JSONActivity.this, name, 1000).show();
                    }
            } catch (JSONException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
    
        }
    }
  • 相关阅读:
    用电脑给手机安装App
    切换皮肤的实现
    瀑布流的简单实现
    HTML5的实用
    HTML5的特性,发展,及使用
    录音的使用步骤
    支付宝集成步骤
    美团(iPad)顶部界面的简单实现, 及开发时常见bug
    真机调试/打包测试/程序发布/内购的具体操作流程
    IOS 触摸事件的处理
  • 原文地址:https://www.cnblogs.com/engine1984/p/4227854.html
Copyright © 2011-2022 走看看