快捷搜索:  as  2018  FtCWSyGV  С˵  test  xxx  Ψһ  w3viyKQx

澳门新葡萄app下载_龟发之家论坛



再便是在Activity中添加4个按钮,分手相应这四次解析,结构相对简单,仅四个按钮,此处不给出结构代码,有兴趣的可以下载源码查看。由于是Android4.0+的平台,以是不能在主线程中造访收集,必要另起线程,还有别忘怀增添造访收集的权限。

Activity代码:

package cn.bgxt.jsonparser;

import java.util.List;

import java.util.Map;

import cn.bgxt.domain.Person;import cn.bgxt.http.HttpUtils;

import cn.bgxt.jsonUtils.JsonTools;import android.os.Bundle;

import android.app.Activity;import android.util.Log;

import android.view.Menu;import android.view.View;

import android.view.View.OnClickListener;import android.widget.Button;

public class MainActivity extends Activity implements OnClickListener {

private String path_url = "http://192.168.1.106:1231/json/returnJson.ashx?JsonData=";

private Button person, personList, stringList, dicList;private String path = "";

@Override

protected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

person = (Button) findViewById(R.id.person);personList = (Button) findViewById(R.id.personList);

stringList = (Button) findViewById(R.id.stringList);dicList = (Button) findViewById(R.id.dicList);

person.setOnClickListener(this);personList.setOnClickListener(this);

stringList.setOnClickListener(this);dicList.setOnClickListener(this);

}

@Overridepublic boolean onCreateOptionsMenu(Menu澳门新葡萄app下载 menu) {

getMenuInflater().inflate(R.menu.main, menu);return true;

}

@Overridepublic void onClick(View v) {

switch (v.getId()) {

case R.id.person:Thread thread = new Thread(new Runnable() {

@Override

public void run() {// TODO Auto-generated method stub

path = path_url + "Person";String jsonString = HttpUtils.getJsonContent(path);

Person person = JsonTools.getPerson(jsonString);Log.i("main", person.toString());

}澳门新葡萄app下载});

thread.start();break;

case R.id.personList:Thread thread1 = new Thread(new Runnable() {

@Overridepublic void run() {

// TODO Auto-generated method stubpath = path_url + "PersonList";

String jsonString = HttpUtils.getJsonContent(path);List

persons = JsonTools.getPersonList(jsonString);

for(Person p:persons){

Log.i("main",p.toString());}

}});

thread1.start();break;

case R.id.stringList:Thread thread2 = new Thread(new Runnable() {

@Overridepublic void run() {

// TODO Auto-generated method stubpath = path_url + "StringList";

String jsonString = HttpUtils.getJsonContent(path);List strings = JsonTools.getStringList(jsonString);

for(String s:strings){

Log.i("main",s);}

}});

thread2.start();break;

case R.id.dicList:Thread thread3 = new Thread(new Runnable() {

@Overridepublic void run() {

// TODO Auto-generated method stubpath = path_url + "DicList";

String jsonString = HttpUtils.getJsonContent(path);List> maps = JsonTools.getMapList(jsonString);

for(Map m:maps){

Log.i("main",m.toString());}

}});

thread3.start();break;

}

}

}

依次按顺序点击按钮,可以再LogCat中查看数据:

示例法度榜样下载

总结

原先Java中有一套自己的序列化法子,序列化出来的Json数据并不是如斯的款式,可这里应用的.Net序列化的法子,然则参照本文的思惟,只要知道Json的款式,无论Json数据变什么样子,都是可以解析出来的。真实的Android开拓办事真个时刻,照样只管即便应用Java来开拓,削减兼容性的问题。

从IIS上获取Json数据的措施,在别的一篇中有先容:Http协议。这里方便大年夜家查看:

package cn.bgxt.http;

import java.io.ByteArrayOutputStream;

import java.io.IOException;import java.io.InputStream;

import java.net.HttpURLConnection;import java.net.URL;

import android.util.Log;

public class HttpUtils {public HttpUtils() {

// TODO Auto-generated constructor stub}

public static String getJsonContent(String url_path){

try {URL url=new URL(url_path);

HttpURLConnection connection=(HttpURLConnection)url.openConnection();connection.setConnectTimeout(3000);

connection.setRequestMethod("GET");connection.setDoInput(true);

int code=connection.getResponseCode();if(200==code)

{return changeJsonString(connection.getInputStream());

}} catch (Exception e) {

// TODO: handle exceptionLog.i("main", e.getMessage());

}return null;

}

private static String changeJsonString(InputStream inputStream) {String jsonString="";

try {ByteArrayOutputStream outputStream=new ByteArrayOutputStream();

int len=0;byte[] data=new byte[1024];

while((len=inputStream.read(data))!=-1){

outputStream.write(data,0,len);}

jsonString=new String(outputStream.toByteArray());} catch (IOException e) {

// TODO Auto-generated catch blocke.printStackTrace();

}return jsonString;

}}

从IIS上获取到Json数据之后,必要新建一个类来解析办事端获取到的Json数据:

package cn.bgxt.jsonUtils;

import java.util.ArrayList;

import java.util.HashMap;import java.util.List;

import java.util.Map;

import org.json.JSONArray;import org.json.JSONObject;

import cn.bgxt.domain.Person;

public class JsonTools {

public JsonTools() {

// TODO Auto-generated constructor stub}

public static Person getPerson(String jsonString)

{Person person=new Person();

try {//一个Json工具,应用JSONObject

JSONObject jsonObject=new JSONObject(jsonString);//对JSONObject工具中直接以key的形式取其Value

person.setId(jsonObject.getInt("id"));person.setName(jsonObject.getString("name"));

person.setAge(jsonObject.getInt("age"));} catch (Exception e) {

// TODO: handle exception}

return person;}

public static List

getPersonList(String jsonString)

{List

list=new ArrayList

();

try {//最外层是一个数组[],以是应用JSONArray

JSONArray jsonarr=new JSONArray(jsonString);Person person=null;

for(int i=0;i{

//遍历数组得到数组中Json工具。person=new Person();

JSONObject jsonObject= jsonarr.getJSONObject(i);//获取到Json工具,就可以直接经由过程Key获取Value

person.setId(json澳门新葡萄app下载Object.getInt("id"));person.setName(jsonObject.getString("name"));

person.setAge(jsonObject.getInt("age"));list.add(person);

}} catch (Exception e) {

// TODO: handle exception}

return list;

}

public static List getStringList(String jsonString){

List list=new ArrayList();try {

JSONArray jsonArray=new JSONArray(jsonString);for(int i =0;i

{String str=jsonArray.getString(i);

list.add(str);}

} catch (Exception e) {// TODO: handle exception

}return list;

}

public static List> getMapList(String jsonString){

List> maps=new ArrayList>();try {

//最外层是一个Json数组JSONArray jsonArray=new JSONArray(jsonString);

for(int i =0;i{

//Json数组中又包孕了一个Json数组Map map=new HashMap();

JSONArray mapArray=jsonArray.getJSONArray(i);for(int j=0;j

{//第二层Json数组中才能取到Json工具,澳门新葡萄app下载有工具就可以取值。

JSONObject jsonObject=mapArray.getJSONObject(j);String key=jsonObject.getString("Key");

Object value=jsonObject.get("Value");map.put(key, value);

}maps.add(map);

}} catch (Exception e) {

// TODO: handle exception}

return maps;

}澳门新葡萄app下载

}

您可能还会对下面的文章感兴趣: