MasterofProject

Android development in the JSON data analysis tools

Label Android DevelopmentJSON parsingWorking class
344 people read comment(2) Collection Report
Classification:

Processing JSON data ---------------------------------------------------------------- ----------------------------------------------------- with Android comes with JSONArray and jsonobject

In the following method the incoming path String is the URL address of the server where the data is located

Class dealJsonUtil public {

*
* get the JSON data of the array form,
* /
Static List<Map<String public, getJSONArray String>> (path String) Exception throws {
JSON String = null;
String>>, list List<Map<String = ArrayList<Map<String String>>, new ();
String>, map Map<String = null;
URL URL = URL new (path);
The /**HttpURLConnection object, the web data acquisition from the network.
Conn HttpURLConnection = (HttpURLConnection) url.openConnection ();
/ * * set the timeout time is 5 seconds.
Conn.setConnectTimeout (1000 * 5);
/**HttpURLConnection is through the HTTP protocol to request the path path, so you need to set the request method, you can not set, because the default is GET*/
Conn.setRequestMethod ("GET");
To determine whether the request / * * success, success or failure to request code 200.
(if) (conn.getResponseCode = = 200) {
Get the data input stream * / / * *
Is InputStream = conn.getInputStream ();
The input stream into / * * * character array
Data byte[] = readStream2Array (is);
Conversion / * * character array into a string.
JSON = String new (data);
*
Name * data form: [{"stuNo: 100:" Xiao Ming "}, {" stuNo ": 101," name ":" Zhang "}] data array, directly with the Android framework JSONArray read data, converted into array
* /
JsonArray JSONArray = JSONArray new (JSON);
For (I int = 0; I < jsonArray.length (); i++) {
Each object in the data acquisition / * * * /
Item JSONObject = jsonArray.getJSONObject (I);
Note that the key value should be consistent * / / * *
Id int = item.getInt ("stuNo");
Name String = item.getString ("name");
Map = HashMap<String String>, new ();
Map.put ("stuNo", ID + "");
Map.put ("name", name);
List.add (map);
}
}
List return;
}


/ * *
* get "object form" JSON data,
*
* path @param page path
* @return returns List
* Exception @throws
* /
Static List<Map<String public, getJSONObject String>> (path String) Exception throws {
String>>, list List<Map<String = ArrayList<Map<String String>>, new ();
String>, map Map<String = null;
URL URL = URL new (path);
Conn HttpURLConnection = (HttpURLConnection) url.openConnection ();
Conn.setConnectTimeout (5 * 1000); / / set the timeout in milliseconds, the time for 5 seconds
Conn.setRequestMethod ("GET");
(if) (conn.getResponseCode = = 200) {
Is InputStream = conn.getInputStream ();
Data byte[] = readStream2Array (is);
JSON String = String new (data);
*
* data form: {"total": 2, true: "success", "arrayData": [{"Id": 1, "name", "pig"}, {"Id": 2, "name": "cat"}]}
* the returned data form is a Object type, so it can be converted directly into a Object*/
JsonObject JSONObject = JSONObject new (JSON);
Total int = jsonObject.getInt ("total");
Success Boolean = jsonObject.getBoolean ("success");
There is a /**json array of data objects, and can be used to obtain getJSONArray array.
JsonArray JSONArray = jsonObject.getJSONArray ("arrayData");
For (I int = 0; I < jsonArray.length (); i++) {
Item JSONObject = jsonArray.getJSONObject (I);
Id int = item.getInt ("Id");
Name String = item.getString ("name");
Map = HashMap<String String>, new ();
Map.put ("Id", ID + "");
Map.put ("name", name);
List.add (map);
}
}
List return;
}


/ * *
* get type complex JSON data
* /
Static List<Map<String public, getComplexJSON String>> (path String) Exception throws {
地图列表字符串字符串<<>,<<ArrayList的地图列表=新的字符串,字符串> >();
图> <字符串,字符串地图= null;
URL的URL =新的URL(路径);
httpurlconnection conn =(httpurlconnection)url.openconnection();
conn.setconnecttimeout(5×1000);
conn.setrequestmethod(“get”);
如果(conn.getresponsecode)= {(200)
conn.getinputstream InputStream =();
字节[]数据= readstream2array(IS);
JSON字符串= new String(数据);
/*
*数据形式:{“名字”:“小猪23”、“时代”、“内容”:::{“questionstotal”2、“问题”:{“问题”:“你叫什么名字?”“答案”:“小猪”},{”的问题:“什么是你的“时代”,“答案”:“23”} } }
*/
jsonobject jsonobject =新的jsonobject(JSON);
jsonobject.getstring字符串(“name”);
=(int jsonobject.getint时代的“时代”);
log.i(“ABC”、“姓名:”+“+姓名+年龄|时代:”);
jsonobject contentobject = jsonobject.getjsonobject(“内容”);
questionstotal contentobject.getstring(=“字符串questionstotal”);
jsonarray contentarray = contentobject.getjsonarray(“网站”);
for(int i=0;i<contentarray.length();i++){
jsonobject item = contentarray.getjsonobject(i);
字符串的问题item.getstring(=“问题”);
答案= item.getstring字符串(“答案”);
图=新的字符串的字符串(HashMap的<,>);
map.put(“问题”的问题);
map.put(“答案”,答案);
list.add(MAP);
}
}
返回列表;
}


*
*把输入流转换成字符数组
*/
公共静态字节readstream2array(InputStream类型的异常(InputStream)
bytearrayoutputstream Bout =新的bytearrayoutputstream();
=新的字节缓冲区的字节[] [ 1024〕;
int len=0;
while((LEN=inputstream.read(缓冲区)。(1){
bout.write(buffer,0,();
}
bout.close();
inputstream.close();
bout.tobytearray();return
}
}
0
0
猜你在找
查看评论
*以上用户言论只代表其个人观点,不代表csdn网站的观点或立场
    个人资料
    • 访问:42339次
    • 积分:2059
    • 等级:
    • 排名:第11126名
    • 原创:169篇
    • 转载:36篇
    • 原文:1篇
    • 评论:9条
    文章分类
    文章存档
    最新评论