zoukankan      html  css  js  c++  java
  • 【Android】解析Json数据

    Json数据:"{"UserID":"Allen","Dep":IT,"QQ":"969661314"}"

    通过如下代码,将此Json数据转换为Json对象,类似数组一样,然后通过字段名获取每一个值:

    package com.example.androidjson;
    
    import org.json.JSONException;
    import org.json.JSONObject;
     
    import android.support.v7.app.ActionBarActivity;
    import android.os.Bundle;
    import android.view.Menu;
    import android.view.MenuItem;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;
    import android.widget.TextView;
    
    
    public class MainActivity extends ActionBarActivity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            
            
            Button btn=(Button)findViewById(R.id.btnJson);
            
            btn.setOnClickListener(new OnClickListener() {
                
                @Override
                public void onClick(View arg0) {
                    // TODO Auto-generated method stub
                    
                     String myjson="{"UserID":"Allen","Dep":IT,"QQ":"969661314"}";
                     
                     try {
                        JSONObject json=new JSONObject(myjson);
                        
                        String strUserID=json.getString("UserID");
                        String strDep=json.getString("Dep");
                        String strQQ=json.getString("QQ");
                        
                        TextView txtUserID=(TextView)findViewById(R.id.textView1);
                        TextView txtDep=(TextView)findViewById(R.id.textView2);
                        TextView txtQQ=(TextView)findViewById(R.id.textView3);
                        txtUserID.setText(strUserID);
                        txtDep.setText(strDep);
                        txtQQ.setText(strQQ);
                        
                    } catch (JSONException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                     
                }
            });
            
            
            
            
        }
    
    
        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            // Inflate the menu; this adds items to the action bar if it is present.
            getMenuInflater().inflate(R.menu.main, menu);
            return true;
        }
    
        @Override
        public boolean onOptionsItemSelected(MenuItem item) {
            // Handle action bar item clicks here. The action bar will
            // automatically handle clicks on the Home/Up button, so long
            // as you specify a parent activity in AndroidManifest.xml.
            int id = item.getItemId();
            if (id == R.id.action_settings) {
                return true;
            }
            return super.onOptionsItemSelected(item);
        }
    }

  • 相关阅读:
    Linux Virtual Server技术
    log4j+slf4j迁移到log4j2+slf4j (Servlet3.0)
    Android控件ToggleButton的使用方法
    Redis学习手冊(事务)
    游戏server之server优化思路
    Codeforces 474D Flowers (线性dp 找规律)
    【C语言】编写函数实现库函数atof
    unity3D游戏开发实战原创视频讲座系列7之消消乐游戏开发
    [WebGL入门]二十一,从平行光源发出的光
    TwoSum leetcode
  • 原文地址:https://www.cnblogs.com/allen0118/p/4836913.html
Copyright © 2011-2022 走看看