溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

JSONObject與JSONArray如何在Java中使用

發(fā)布時間:2021-01-21 16:44:56 來源:億速云 閱讀:194 作者:Leah 欄目:編程語言

本篇文章給大家分享的是有關(guān)JSONObject與JSONArray如何在Java中使用,小編覺得挺實用的,因此分享給大家學(xué)習(xí),希望大家閱讀完這篇文章后可以有所收獲,話不多說,跟著小編一起來看看吧。

1.JSONObject與JSONArray使用的場景區(qū)別;

  1. 想通過鍵值對的形式獲取數(shù)據(jù),使用JSONObject。

  2. 如果后臺查詢的是某個bean的list集合向前端頁面?zhèn)鬟f,使用JSONArray。

2. JSONObject與JSONArray使用方法區(qū)別;

 創(chuàng)建方法不同:

 JSONObject創(chuàng)建的方法:

    //創(chuàng)建JsonObject第一種方法
  JSONObject jsonObject = new JSONObject();
  jsonObject.put("UserName", "kobi");
  jsonObject.put("age", "34");
  jsonObject.put("workIn", "ALI");
  System.out.println("jsonObject1:" + jsonObject);

  //創(chuàng)建JsonObject第二種方法
  HashMap<String, String> hashMap = new HashMap<String, String>();
  hashMap.put("UserName", "ZHULI");
  hashMap.put("age", "30");
  hashMap.put("workIn", "ALI");
  System.out.println("jsonObject2:" + JSONObject.fromObject(hashMap));

JSONArray創(chuàng)建的方法

    //創(chuàng)建一個JsonArray方法1
  JSONArray jsonArray = new JSONArray();
  jsonArray.add(0, "kobi");
  jsonArray.add(1, "34");
  jsonArray.add(2, "ALI");
  System.out.println("jsonArray1:" + jsonArray);
  
  //創(chuàng)建JsonArray方法2
  ArrayList<String> arrayList = new ArrayList<String>();
  arrayList.add("kobi");
  arrayList.add("34");
  arrayList.add("ALI");
  System.out.println("jsonArray2:" + JSONArray.fromObject(arrayList));

獲取方式不同

  1. 獲取JSONObject中值:String userName = jsonObject.getString("UserName");

  2. 獲取JSONArray中的值:String userName = arrayList.getString("2");

示例

package com.yunos.tv.video.resource.controller.web;

import java.util.ArrayList;
import java.util.HashMap;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;


public class Test {

 public static void main(String[] args) {
  //JsonObject和JsonArray區(qū)別就是JsonObject是對象形式,JsonArray是數(shù)組形式
  //創(chuàng)建JsonObject第一種方法
  JSONObject jsonObject = new JSONObject();
  jsonObject.put("UserName", "ZHULI");
  jsonObject.put("age", "30");
  jsonObject.put("workIn", "ALI");
  System.out.println("jsonObject1:" + jsonObject);
  
  //創(chuàng)建JsonObject第二種方法
  HashMap<String, String> hashMap = new HashMap<String, String>();
  hashMap.put("UserName", "ZHULI");
  hashMap.put("age", "30");
  hashMap.put("workIn", "ALI");
  System.out.println("jsonObject2:" + JSONObject.fromObject(hashMap));
  
  //創(chuàng)建一個JsonArray方法1
  JSONArray jsonArray = new JSONArray();
  jsonArray.add(0, "ZHULI");
  jsonArray.add(1, "30");
  jsonArray.add(2, "ALI");
  System.out.println("jsonArray1:" + jsonArray);
  
  //創(chuàng)建JsonArray方法2
  ArrayList<String> arrayList = new ArrayList<String>();
  arrayList.add("ZHULI");
  arrayList.add("30");
  arrayList.add("ALI");
  System.out.println("jsonArray2:" + JSONArray.fromObject(arrayList));
  
  //如果JSONArray解析一個HashMap,則會將整個對象的放進(jìn)一個數(shù)組的值中
  System.out.println("jsonArray FROM HASHMAP:" + JSONArray.fromObject(hashMap));
  
  //組裝一個復(fù)雜的JSONArray
  JSONObject jsonObject2 = new JSONObject();
  jsonObject2.put("UserName", "ZHULI");
  jsonObject2.put("age", "30");
  jsonObject2.put("workIn", "ALI");
  jsonObject2.element("Array", arrayList);
  System.out.println("jsonObject2:" + jsonObject2);
    
 }
}

結(jié)果:

 jsonObject1:{"UserName":"ZHULI","age":"30","workIn":"ALI"}
jsonObject2:{"workIn":"ALI","age":"30","UserName":"ZHULI"}
jsonArray1:["ZHULI","30","ALI"]
jsonArray2:["ZHULI","30","ALI"]
jsonArray FROM HASHMAP:[{"workIn":"ALI","age":"30","UserName":"ZHULI"}]
jsonObject2:{"UserName":"ZHULI","age":"30","workIn":"ALI","Array":["ZHULI","30","ALI"]}

3. 解析JSON字符串;

package com.yunos.tv.video.resource.controller.web;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;


public class Test {

 public static void main(String[] args) {
  String jsonString = "{\"UserName\":\"kobi\",\"age\":\"34\",\"workIn\":\"ALI\",\"Array\":[\"kobi\",\"34\",\"ALI\"]}";
  //將Json字符串轉(zhuǎn)為java對象
  JSONObject obj = JSONObject.fromObject(jsonString);
  //獲取Object中的UserName
  if (obj.has("UserName")) {
   System.out.println("UserName:" + obj.getString("UserName"));
  }
  //獲取ArrayObject
  if (obj.has("Array")) {
   JSONArray transitListArray = obj.getJSONArray("Array");
   for (int i = 0; i < transitListArray.size(); i++) {
    System.out.print("Array:" + transitListArray.getString(i) + " ");
   }
  }
 }
}

返回值:

UserName:kobi
Array:kobi Array:34 Array:ALI

以上就是JSONObject與JSONArray如何在Java中使用,小編相信有部分知識點可能是我們?nèi)粘9ぷ鲿姷交蛴玫降?。希望你能通過這篇文章學(xué)到更多知識。更多詳情敬請關(guān)注億速云行業(yè)資訊頻道。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI