本文介绍Java中如果处理Json。例如json编码与解码。以上节选自《Netkiller Java 手札》
目录
package netkiller.json;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import javax.json.*;
public final class Writer {
public static void main(String[] args) {
// TODO Auto-generated method stub
JsonObjectBuilder jsonBuilder = Json.createObjectBuilder();
JsonObjectBuilder addressBuilder = Json.createObjectBuilder();
JsonArrayBuilder phoneNumBuilder = Json.createArrayBuilder();
phoneNumBuilder.add("12355566688").add("0755-2222-3333");
addressBuilder.add("street", "Longhua").add("city", "Shenzhen").add("zipcode", 518000);
jsonBuilder.add("nickname", "netkiller").add("name", "Neo").add("department", "IT").add("role", "Admin");
jsonBuilder.add("phone", phoneNumBuilder);
jsonBuilder.add("address", addressBuilder);
JsonObject jsonObject = jsonBuilder.build();
System.out.println(jsonObject);
try {
// write to file
File file = new File("json.txt");
if (!file.exists()) {
file.createNewFile();
}
OutputStream os = null;
os = new FileOutputStream(file);
JsonWriter jsonWriter = Json.createWriter(os);
jsonWriter.writeObject(jsonObject);
jsonWriter.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
运行后输出
{"nickname":"netkiller","name":"Neo","department":"IT","role":"Admin","phone":["12355566688","0755-2222-3333"],"address":{"street":"Longhua","city":"Shenzhen","zipcode":"518000"}}
package netkiller.json;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import javax.json.Json;
import javax.json.JsonArray;
import javax.json.JsonObject;
import javax.json.JsonReader;
import javax.json.JsonValue;
public final class Reader {
public static final String JSON_FILE="json.txt";
public static void main(String[] args) throws IOException {
InputStream fis = new FileInputStream(JSON_FILE);
//create JsonReader object
JsonReader jsonReader = Json.createReader(fis);
//get JsonObject from JsonReader
JsonObject jsonObject = jsonReader.readObject();
//we can close IO resource and JsonReader now
jsonReader.close();
fis.close();
System.out.printf("nickname: %s \n", jsonObject.getString("nickname"));
System.out.printf("name: %s \n", jsonObject.getString("name"));
System.out.printf("department: %s \n", jsonObject.getString("department"));
System.out.printf("role: %s \n", jsonObject.getString("role"));
JsonArray jsonArray = jsonObject.getJsonArray("phone");
//long[] numbers = new long[jsonArray.size()];
int index = 0;
for(JsonValue value : jsonArray){
//numbers[index++] = Long.parseLong(value.toString());
System.out.printf("phone[%d]: %s \n", index++, value.toString());
}
//reading inner object from json object
JsonObject innerJsonObject = jsonObject.getJsonObject("address");
System.out.printf("address: %s, %s, %d \n", innerJsonObject.getString("street"), innerJsonObject.getString("city"), innerJsonObject.getInt("zipcode"));
}
}
运行结果
nickname: netkiller
name: Neo
department: IT
role: Admin
phone[0]: +8612355566688
phone[1]: 0755-2222-3333
address: Longhua, Shenzhen, 518000
扫码关注腾讯云开发者
领取腾讯云代金券
Copyright © 2013 - 2025 Tencent Cloud. All Rights Reserved. 腾讯云 版权所有
深圳市腾讯计算机系统有限公司 ICP备案/许可证号:粤B2-20090059 深公网安备号 44030502008569
腾讯云计算(北京)有限责任公司 京ICP证150476号 | 京ICP备11018762号 | 京公网安备号11010802020287
Copyright © 2013 - 2025 Tencent Cloud.
All Rights Reserved. 腾讯云 版权所有