ตัว JSONPointer เป็นมาตรฐานที่กำหนด ไวยากรณ์สตริง ที่สามารถใช้ในการเข้าถึงค่าคีย์เฉพาะในเอกสาร JSON ตัวอย่างของ JSONPointer สามารถสร้างได้โดยการเรียกเมธอดโรงงานแบบคงที่ createPointer() บน Json ระดับ. ใน JSONPointer ไวยากรณ์สตริงทุกคำนำหน้าด้วย “/” . เราสามารถรับค่าของคีย์ได้โดยการเรียก getValue() วิธีการใน JsonPointer วัตถุ
ไฟล์ JSON

ตัวอย่าง
import javax.json.*;
import java.io.*;
public class JsonPointerTest {
public static void main(String[] args) throws Exception {
JsonReader jsonReader = Json.createReader(new FileReader("simple.json"));
JsonStructure jsonStructure = jsonReader.read();
JsonPointer jsonPointer1 = Json.createPointer("/firstName");
JsonString jsonString = (JsonString)jsonPointer1.getValue(jsonStructure);
System.out.println("First Name: " + jsonString.getString()); // prints first name
JsonPointer jsonPointer2 = Json.createPointer("/phoneNumbers");
JsonArray array = (JsonArray)jsonPointer2.getValue(jsonStructure);
System.out.println("Phone Numbers:");
for(JsonValue value : array) {
JsonObject objValue = (JsonObject)value;
System.out.println(objValue.toString()); // prints phone numbers
}
JsonPointer jsonPointer3 = Json.createPointer("/phoneNumbers/1");
JsonObject jsonObject1 = (JsonObject)jsonPointer3.getValue(jsonStructure);
System.out.println("Home: " + jsonObject1.toString()); // prints home phone number
JsonPointer jsonPointer4 = Json.createPointer("");
JsonObject jsonObject2 = (JsonObject)jsonPointer4.getValue(jsonStructure);
System.out.println("JSON:\n" + jsonObject2.toString()); // prints JSON structure
jsonReader.close();
}
} ผลลัพธ์
First Name: Raja
Phone Numbers:
{"Mobile":"9959984000"}
{"Home":"0403758000"}
Home: {"Home":"0403758000"}
JSON:
{"firstName":"Raja","lastName":"Ramesh","age":30,"streetAddress":"Madhapur","city":"Hyderabad","state":"Telangana","phoneNumbers":[{"Mobile":"9959984000"},{"Home":"0403758000"}]}