欢迎来到代码驿站!

JAVA代码

当前位置:首页 > 软件编程 > JAVA代码

Java中JSONObject与JSONArray的使用区别详解

时间:2022-04-23 08:45:59|栏目:JAVA代码|点击:

最近公司开发的几个项目中,后台Action向前端传递数据都是Json格式,于是对JSONObject、JSONArray简单的研究了一下,废话不多说,想使用JSONObject、JSONArray,策则在项目中必须要有commons-lang.jar commons-beanutils.jar commons-collections.jar commons-logging.jar  ezmorph.jar json-lib-2.2.2-jdk15.jar 这些Jar包。

1.JSONObject与JSONArray使用的场景区别;

  1. 想通过键值对的形式获取数据,使用JSONObject。
  2. 如果后台查询的是某个bean的list集合向前端页面传递,使用JSONArray。

2. JSONObject与JSONArray使用方法区别;

 创建方法不同:

 JSONObject创建的方法:

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

  //创建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创建的方法

    //创建一个JsonArray方法1
  JSONArray jsonArray = new JSONArray();
  jsonArray.add(0, "kobi");
  jsonArray.add(1, "34");
  jsonArray.add(2, "ALI");
  System.out.println("jsonArray1:" + jsonArray);
  
  //创建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区别就是JsonObject是对象形式,JsonArray是数组形式
  //创建JsonObject第一种方法
  JSONObject jsonObject = new JSONObject();
  jsonObject.put("UserName", "ZHULI");
  jsonObject.put("age", "30");
  jsonObject.put("workIn", "ALI");
  System.out.println("jsonObject1:" + jsonObject);
  
  //创建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方法1
  JSONArray jsonArray = new JSONArray();
  jsonArray.add(0, "ZHULI");
  jsonArray.add(1, "30");
  jsonArray.add(2, "ALI");
  System.out.println("jsonArray1:" + jsonArray);
  
  //创建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,则会将整个对象的放进一个数组的值中
  System.out.println("jsonArray FROM HASHMAP:" + JSONArray.fromObject(hashMap));
  
  //组装一个复杂的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);
    
 }
}

结果:

 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字符串转为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

上一篇:教你如何将Springboot项目成功部署到linux服务器

栏    目:JAVA代码

下一篇:聊聊在获取方法参数名方面,Spring真的就比Mybatis强?

本文标题:Java中JSONObject与JSONArray的使用区别详解

本文地址:http://www.codeinn.net/misctech/199981.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有