JAVA中的JSON

來源:互聯網
上載者:User

標籤:解決辦法   rip   轉換   訪問   根據   sonar   imp   ges   book   

1、在伺服器端的Servlet類中,可以使用如下方法收集資料並產生相應的JSON字串

//聲明一個Hash對象並添加資料
Map params = new HashMap();

params.put("username", username);
params.put("user_json", user);

//聲明JSONArray對象並輸入JSON字串
JSONArray array = JSONArray.fromObject(params);
put.println(array.toString());

2、在WEB前端可以通過javascript直接對JSON字串進行解析,在Android用戶端的話,需要使用JSON類來解析字串:

//@description: 根據接收到的JSON字串來解析字串中所包含的資料和資料對象

//接收到的JSON字串
String result = "[{\"username\": \"your name\", \"user_json\": {\"username\": \"your name\", \"nickname\": \"your nickname\"}}]";

//根據字串產生JSON對象
JSONArray resultArray = new JSONArray(result);
JSONObject resultObj = resultArray.optJSONObject(0);

//擷取資料項目
String username = resultObj.getString("username");

//擷取資料對象
JSONObject user = resultObj.getJSONObject("user_json");
String nickname = user.getString("nickname");

3、資料類型之間的轉換

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

//別忘了添加上JSON包哦!
public class StringToJSON {
public static void main(String[] args) throws JSONException{

System.out.println("abc");
//定義JSON字串
String jsonStr = "{\"id\": 2," +
" \"title\": \"json title\", " +
"\"config\": {" +
"\"width\": 34," +
"\"height\": 35," +
"}, \"data\": [" +
"\"JAVA\", \"JavaScript\", \"PHP\"" +
"]}";

//轉換成為JSONObject對象
JSONObject jsonObj = new JSONObject(jsonStr);

//從JSONObject對象中擷取資料
JavaBean bean = new JavaBean();

//根據屬性名稱擷取int型資料;
bean.setId(jsonObj.getInt("id"));

//根據屬性名稱擷取String資料;
bean.setTitle(jsonObj.getString("title"));

//根據屬性名稱擷取JSONObject類
JSONObject config = jsonObj.getJSONObject("config");
bean.setWidth(config.getInt("width"));
bean.setHeight(config.getInt("height"));

//根據屬性名稱擷取JSONArray數組
JSONArray data = jsonObj.getJSONArray("data");
for(int index = 0, length = data.length(); index < length; index++) {
//這裡在org.json.JSONArray對象中居然沒有找到toArray方法,求各位網友給出解決辦法啊!
// bean.setLanguages(String[]);
}
}
}

class JavaBean{
private int id ;
private String title;
private int width;
private int height;
private String[] languages;

//這裡省略了設定器和訪問器
}

4、JSON對象轉換為String字串

public static void main(String[] args) throws JSONException {

//建立JSONObject對象
JSONObject json = new JSONObject();

//向json中添加資料
json.put("username", "wanglihong");
json.put("height", 12.5);
json.put("age", 24);

//建立JSONArray數組,並將json添加到數組
JSONArray array = new JSONArray();
array.put(json);

//轉換為字串
String jsonStr = array.toString();

System.out.println(jsonStr);
}

 

最終輸出結果為: [{"username":"wanglihong","height":12.5,"age":24}]  

5、集合轉換為JSONArray對象

public static void main(String[] args)throws JSONException{
//初始化ArrayList集合并添加資料
List<String> list = new ArrayList<String>();
list.add("username");
list.add("age");
list.add("sex");

//初始化HashMap集合并添加數組
Map map = new HashMap();
map.put("bookname", "CSS3實戰");
map.put("price", 69.0);

//初始化JSONArray對象,並添加資料
JSONArray array = new JSONArray();
array.put(list);
array.put(map);

//產生的JSON字串為:[["username","age","sex"],{"price":69,"bookname":"CSS3實戰"}]
}

 

JAVA中的JSON

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.