日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

java如何解析json_java 中解析json步骤

發布時間:2024/10/8 编程问答 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java如何解析json_java 中解析json步骤 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

一、?? JSON (JavaScript Object Notation)一種簡單的數據格式,比xml更輕巧。

Json建構于兩種結構:

1、“名稱/值”對的集合(A collection of name/value pairs)。不同的語言中,它被理解為對象(object),紀錄(record),結構(struct),字典(dictionary),哈希表(hash table),有鍵列表(keyed list),或者關聯數組?(associative array)。 如:

{

“name”:”jackson”,

“age”:100

}

2、值的有序列表(An ordered list of values)。在大部分語言中,它被理解為數組(array)如:

{

“students”:

[

{“name”:”jackson”,“age”:100},

{“name”:”michael”,”age”:51}

]

}

二、java解析JSON步驟

A、服務器端將數據轉換成json字符串

首先、服務器端項目要導入json的jar包和json所依賴的jar包至builtPath路徑下(這些可以到JSON-lib官網下載:http://json-lib.sourceforge.net/)

然后將數據轉為json字符串,核心函數是:

public static String createJsonString(String key, Object value)

{

JSONObject jsonObject = new JSONObject();

jsonObject.put(key, value);

return jsonObject.toString();

}

B、客戶端將json字符串轉換為相應的javaBean

1、客戶端獲取json字符串(因為android項目中已經集成了json的jar包所以這里無需導入)

public class HttpUtil

{

public static String getJsonContent(String urlStr)

{

try

{// 獲取HttpURLConnection連接對象

URL url = new URL(urlStr);

HttpURLConnection httpConn = (HttpURLConnection) url

.openConnection();

// 設置連接屬性

httpConn.setConnectTimeout(3000);

httpConn.setDoInput(true);

httpConn.setRequestMethod("GET");

// 獲取相應碼

int respCode = httpConn.getResponseCode();

if (respCode == 200)

{

return ConvertStream2Json(httpConn.getInputStream());

}

}

catch (MalformedURLException e)

{

// TODO Auto-generated catch block

e.printStackTrace();

}

catch (IOException e)

{

// TODO Auto-generated catch block

e.printStackTrace();

}

return "";

}

private static String ConvertStream2Json(InputStream inputStream)

{

String jsonStr = "";

// ByteArrayOutputStream相當于內存輸出流

ByteArrayOutputStream out = new ByteArrayOutputStream();

byte[] buffer = new byte[1024];

int len = 0;

// 將輸入流轉移到內存輸出流中

try

{

while ((len = inputStream.read(buffer, 0, buffer.length)) != -1)

{

out.write(buffer, 0, len);

}

// 將內存流轉換為字符串

jsonStr = new String(out.toByteArray());

}

catch (IOException e)

{

// TODO Auto-generated catch block

e.printStackTrace();

}

return jsonStr;

}

}

2、獲取javaBean

public static Person getPerson(String jsonStr)

{

Person person = new Person();

try

{// 將json字符串轉換為json對象

JSONObject jsonObj = new JSONObject(jsonStr);

// 得到指定json key對象的value對象

JSONObject personObj = jsonObj.getJSONObject("person");

// 獲取之對象的所有屬性

person.setId(personObj.getInt("id"));

person.setName(personObj.getString("name"));

person.setAddress(personObj.getString("address"));

}

catch (JSONException e)

{

// TODO Auto-generated catch block

e.printStackTrace();

}

return person;

}

public static List getPersons(String jsonStr)

{

List list = new ArrayList();

JSONObject jsonObj;

try

{// 將json字符串轉換為json對象

jsonObj = new JSONObject(jsonStr);

// 得到指定json key對象的value對象

JSONArray personList = jsonObj.getJSONArray("persons");

// 遍歷jsonArray

for (int i = 0; i < personList.length(); i++)

{

// 獲取每一個json對象

JSONObject jsonItem = personList.getJSONObject(i);

// 獲取每一個json對象的值

Person person = new Person();

person.setId(jsonItem.getInt("id"));

person.setName(jsonItem.getString("name"));

person.setAddress(jsonItem.getString("address"));

list.add(person);

}

}

catch (JSONException e)

{

// TODO Auto-generated catch block

e.printStackTrace();

}

return list;

}

總結

以上是生活随笔為你收集整理的java如何解析json_java 中解析json步骤的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。