Computer >> कंप्यूटर ट्यूटोरियल >  >> प्रोग्रामिंग >> Java

Java में @JsonCreator एनोटेशन का उपयोग करके JSON स्ट्रिंग को कैसे डिसीरियलाइज़ करें?


@JsonProperty एनोटेशन का उपयोग JSON में संपत्ति नाम को इंगित करने के लिए किया जा सकता है। इस एनोटेशन का उपयोग कंस्ट्रक्टर या फ़ैक्टरी विधि के लिए किया जा सकता है। @JsonCreator एनोटेशन उन स्थितियों में उपयोगी है जहां @JsonSetter एनोटेशन का उपयोग नहीं किया जा सकता। उदाहरण के लिए, अपरिवर्तनीय ऑब्जेक्ट्स में कोई सेटर विधि नहीं होती है, इसलिए उन्हें कंस्ट्रक्टर में अपने प्रारंभिक मान इंजेक्ट करने की आवश्यकता होती है।

@JsonProperty - कंस्ट्रक्टर

उदाहरण

import com.fasterxml.jackson.annotation.*; import java.io.IOException; import com.fasterxml.jackson.databind.*; public class JsonCreatorTest1 { public static void main(String[] args) throws IOException { ObjectMapper om = new ObjectMapper(); String jsonString = "{\"id\":\"101\", \"fullname\":\"Ravi Chandra\", \"location\":\"Pune\"}"; System.out.println("JSON: " + jsonString); Customer customer = om.readValue(jsonString, Customer.class); System.out.println(customer); } } // Customer class class Customer { private String id; private String name; private String address; public Customer() {}  @JsonCreator  public Customer(  @JsonProperty("id") String id,  @JsonProperty("fullname") String name,  @JsonProperty("location") String address) { this.id = id; this.name = name; this.address = address; }  @Override  public String toString() { return "Customer [id=" + id + ", name=" + name + ", address=" + address + "]"; } }

आउटपुट

JSON: {"id":"101", "fullname":"Ravi Chandra", "location":"Pune"} Customer [id=101, name=Ravi Chandra, address=Pune]

@JsonCreator - फ़ैक्टरी विधि

उदाहरण

import com.fasterxml.jackson.annotation.*; import java.io.IOException; import com.fasterxml.jackson.databind.*; public class JsonCreatorTest2 { public static void main(String[] args) throws IOException { ObjectMapper mapper = new ObjectMapper(); String jsonString = "{\"id\":\"102\", \"fullname\":\"Raja Ramesh\", \"location\":\"Hyderabad\"}"; System.out.println("JSON: " + jsonString); Customer customer = mapper.readValue(jsonString, Customer.class); System.out.println(customer); } } // Customer class class Customer { private String id; private String name; private String address; public Customer() {}  @JsonCreator  public static Customer createCustomer(  @JsonProperty("id") String id,  @JsonProperty("fullname") String name,  @JsonProperty("location") String address) { Customer customer = new Customer(); customer.id = id; customer.name = name; customer.address = address; return customer; }  @Override  public String toString() { return "Customer [id=" + id + ", name=" + name + ", address=" + address + "]"; } }

आउटपुट

JSON: {"id":"101", "fullname":"Raja Ramesh", "location":"Hyderabad"} Customer [id=102, name=Raja Ramesh, address=Hyderabad]

  1. Java में Gson लाइब्रेरी का उपयोग करके JSON स्ट्रिंग को फ़ाइल में कैसे लिखें? Java में Gson लाइब्रेरी का उपयोग करके JSON स्ट्रिंग को फ़ाइल में कैसे लिखें?

    Gson एक लाइब्रेरी है जिसका उपयोग Java ऑब्जेक्ट्स को JSON प्रतिनिधित्व में बदलने के लिए किया जा सकता है। उपयोग की जाने वाली प्राथमिक क्लास Gson है जिसे हम new Gson() को कॉल करके बना सकते हैं और GsonBuilder क्लास का उपयोग Gson इंस्टेंस बनाने के लिए किया जा सकता है। हम नीचे दिए गए उदाहरण में Gson क्ला

  1. - Python का उपयोग करके String को JSON में Convert कैसे करें?

    JSON String को Dictionary में Convert करना JSON String को Dictionary में Convert करने के लिए json.loads() का उपयोग करें। यह Method Valid JSON String Accept करता है और Dictionary Return करता है जिसमें आप सभी Elements Access कर सकते हैं। उदाहरण >>> import json >>> s = {success: tru

  1. - Java गाइड: API टेस्टिंग के लिए JSON फ़ाइल को String में पढ़ें

    इस पोस्ट में हम देखेंगे कि Java में JSON फ़ाइल को String वेरिएबल के रूप में कैसे पढ़ें। यह कभी-कभी उपयोगी होता है, विशेष रूप से API टेस्टिंग में जब आप किसी एंडपॉइंट पर JSON पेलोड POST करना चाहते हैं। आप JSON पेलोड को एक फ़ाइल में रख सकते हैं, फिर JSON फ़ाइल को String के रूप में पढ़ें और इसे POST अनु