java json data_java中json数据格式的处理

json基础

json表示法是一种轻量级的基于文本的开放标准

json是javascript object notation的缩写

json的网络媒体格式是 application/json

容易阅读和编写

语言无关性

json的几种数据格式

对象

An object is an unordered set of name/value pairs. An object begins with { (left brace) and ends with } (right brace). Each name is followed by : (colon) and the name/value pairs are separated by .

1b087b23250f

object

数组

An array is an ordered collection of values. An array begins with [ (left bracket) and ends with ] (right bracket). Values are separated by ,

1b087b23250f

array

A value can be a string in double quotes, or a number, or true or false or null, or an object or an array. These structures can be nested.

1b087b23250f

value

json语法

数据使用键值对表示

使用大括号表示对象,每个名称后面跟着一个:,键值对之间使用,分割

使用方括号[]保存数组,数组值使用,分割

{

"book": [

{

"id":"01",

"language": "Java",

"edition": "third",

"author": "Herbert Schildt"

},

{

"id":"07",

"language": "C++",

"edition": "second"

"author": "E.Balagurusamy"

}]

}

book为键,它对应的又是一个json数组对象,json数组用[]包起来:

[

{

"id":"01",

"language": "Java",

"edition": "third",

"author": "Herbert Schildt"

},

{

"id":"07",

"language": "C++",

"edition": "second"

"author": "E.Balagurusamy"

}

]

数组元素是一个json对象,对象之间使用,隔开:

{

"id":"01",

"language": "Java",

"edition": "third",

"author": "Herbert Schildt"

}

java中使用json

下载 json .jar

/**

* 测试生成json对象

* @throws JSONException

*/

@Test

public void testBuildJsonObject() throws JSONException {

JSONObject jsonObject = new JSONObject();

jsonObject.put("id",1);

jsonObject.put("name","shixu");

jsonObject.put("age",23);

jsonObject.put("id",2); //JsonObject底层是维护一个map,所以不能有重复的键,这里会把之前的值覆盖掉

System.out.println(jsonObject);

System.out.println("id:"+jsonObject.get("id"));

System.out.println("name:"+jsonObject.get("name"));

System.out.println("age:"+jsonObject.get("age"));

HashMap map = new HashMap<>();

map.put("id",1);

map.put("name","shixu");

JSONObject jsonObjectMap= new JSONObject(map); //使用map初始化

System.out.println(jsonObjectMap);

System.out.println("id:"+jsonObjectMap.get("id"));

System.out.println("name:"+jsonObjectMap.get("name"));

}

结果:

{"id":2,"age":23,"name":"shixu"}

id:2

name:shixu

age:23

{"id":1,"name":"shixu"}

id:1

/**

* 测试生成JSONArray ,用于存放jsonObject

* @throws JSONException

*/

@Test

public void testBuildJsonArray() throws JSONException {

JSONArray jsonArray = new JSONArray();

HashMap map = new HashMap<>();

map.put("id",1);

map.put("name","shixu");

JSONObject jsonObjectMap= new JSONObject(map); //使用map初始化

jsonArray.put(jsonObjectMap);

jsonArray.put(jsonObjectMap);

System.out.println(jsonArray);

System.out.println(jsonArray.getJSONObject(0));

System.out.println(jsonArray.getJSONObject(1));

}

结果:

[{"id":1,"name":"shixu"},{"id":1,"name":"shixu"}]

{"id":1,"name":"shixu"}

{"id":1,"name":"shixu"}

将一个json字符串解析为json对象

/**

* 将一个json字符串解析为json对象

* @throws JSONException

*/

@Test

public void testJsonStrToJsonObject() throws JSONException {

String jsonStr = "[{\"id\":1,\"name\":\"shixu\"},{\"id\":2,\"name\":\"suntime\"}]";

JSONTokener jsonTokener = new JSONTokener(jsonStr);

JSONArray jsonArray = new JSONArray(jsonTokener);

for (int i = 0; i

JSONObject jsonObject = jsonArray.getJSONObject(i);

System.out.println(jsonObject);

System.out.print("id:"+jsonObject.get("id"));

System.out.println("\tname:"+jsonObject.get("name"));

}

}

结果:

{"id":1,"name":"shixu"}

id:1 name:shixu

{"id":2,"name":"suntime"}

id:2 name:suntime

总结

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java可以使用许多库来解析JSON文件,其最常用的是Jackson和Gson。以下是使用Gson库读取和处理JSON文件的简单示例: 1. 导入Gson库 ```java import com.google.gson.*; ``` 2. 读取JSON文件 ```java // 从文件读取JSON数据 JsonElement jsonElement = JsonParser.parseReader(new FileReader("data.json")); ``` 3. 处理JSON数据 ```java // 将JSON数据转换为JsonObject JsonObject jsonObject = jsonElement.getAsJsonObject(); // 从JsonObject获取属性值 String name = jsonObject.get("name").getAsString(); int age = jsonObject.get("age").getAsInt(); // 获取嵌套的JsonObject JsonObject addressObject = jsonObject.getAsJsonObject("address"); String city = addressObject.get("city").getAsString(); String state = addressObject.get("state").getAsString(); // 获取JsonArray JsonArray hobbiesArray = jsonObject.getAsJsonArray("hobbies"); List<String> hobbies = new ArrayList<>(); for (JsonElement hobbyElement : hobbiesArray) { hobbies.add(hobbyElement.getAsString()); } ``` 完整示例代码: ```java import com.google.gson.*; import java.io.FileReader; import java.util.ArrayList; import java.util.List; public class JsonExample { public static void main(String[] args) throws Exception { // 从文件读取JSON数据 JsonElement jsonElement = JsonParser.parseReader(new FileReader("data.json")); // 将JSON数据转换为JsonObject JsonObject jsonObject = jsonElement.getAsJsonObject(); // 从JsonObject获取属性值 String name = jsonObject.get("name").getAsString(); int age = jsonObject.get("age").getAsInt(); // 获取嵌套的JsonObject JsonObject addressObject = jsonObject.getAsJsonObject("address"); String city = addressObject.get("city").getAsString(); String state = addressObject.get("state").getAsString(); // 获取JsonArray JsonArray hobbiesArray = jsonObject.getAsJsonArray("hobbies"); List<String> hobbies = new ArrayList<>(); for (JsonElement hobbyElement : hobbiesArray) { hobbies.add(hobbyElement.getAsString()); } // 输出结果 System.out.println("Name: " + name); System.out.println("Age: " + age); System.out.println("City: " + city); System.out.println("State: " + state); System.out.println("Hobbies: " + hobbies); } } ``` 其,假设JSON文件的内容如下: ```json { "name": "Alice", "age": 25, "address": { "city": "New York", "state": "NY" }, "hobbies": [ "reading", "painting", "hiking" ] } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值