【Android】解析Json数据

简介: Json数据:"{\"UserID\":\"Allen\",\"Dep\":IT,\"QQ\":\"969661314\"}" 通过如下代码,将此Json数据转换为Json对象,类似数组一样,然后通过字段名获取每一个值: package com.

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);
    }
}

 

相关文章
|
1月前
|
JSON JavaScript 前端开发
C++ 智能指针与 JSON 处理:高级编程技巧与常见问题解析
C++ 智能指针与 JSON 处理:高级编程技巧与常见问题解析
269 0
|
10天前
|
Android开发 开发者
Android网络和数据交互: 请解释Android中的AsyncTask的作用。
Android's AsyncTask simplifies asynchronous tasks for brief background work, bridging UI and worker threads. It involves execute() for starting tasks, doInBackground() for background execution, publishProgress() for progress updates, and onPostExecute() for returning results to the main thread.
10 0
|
10天前
|
网络协议 安全 API
Android网络和数据交互: 什么是HTTP和HTTPS?在Android中如何进行网络请求?
HTTP和HTTPS是网络数据传输协议,HTTP基于TCP/IP,简单快速,HTTPS则是加密的HTTP,确保数据安全。在Android中,过去常用HttpURLConnection和HttpClient,但HttpClient自Android 6.0起被移除。现在推荐使用支持TLS、流式上传下载、超时配置等特性的HttpsURLConnection进行网络请求。
10 0
|
14天前
|
存储 JSON JavaScript
「Python系列」Python JSON数据解析
在Python中解析JSON数据通常使用`json`模块。`json`模块提供了将JSON格式的数据转换为Python对象(如列表、字典等)以及将Python对象转换为JSON格式的数据的方法。
29 0
|
24天前
|
XML Java Android开发
Android每点击一次按钮就添加一条数据
Android每点击一次按钮就添加一条数据
24 1
|
1月前
|
JSON JavaScript 数据格式
【深入探究C++ JSON库】解析JSON元素的层级管理与遍历手段
【深入探究C++ JSON库】解析JSON元素的层级管理与遍历手段
95 2
|
1月前
|
XML JSON API
深入解析C++ JSON库:nlohmann::json:: parse的内部机制与应用
深入解析C++ JSON库:nlohmann::json:: parse的内部机制与应用
52 0
|
4月前
|
XML JSON Apache
【Android】如何获得Apache服务器的JSON文件数据
【Android】如何获得Apache服务器的JSON文件数据
61 0
|
JSON Java Android开发
android 服务器json
引用:http://www.eoeandroid.com/forum.php?mod=viewthread&tid=69596 首先在服务器端,星空采用的是SSH框架,struts2集合了json插件,服务器和客户端的信息交互采用的JSON来传输,由于在服务器端用了Struts2,所以 星空 就用...
980 0

推荐镜像

更多