1. 程式人生 > 其它 >Java 處理 Json 例項

Java 處理 Json 例項

本文介紹Java中如果處理Json。例如json編碼與解碼。以上節選自《Netkiller Java 手札》

第 5 章 Json

目錄

  • 5.1. Json 編碼
  • 5.2. Json 解碼

5.1. Json 編碼

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"}}		

5.2. Json 解碼

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