JSON से Go Structs
Example
Input:
{"name": "Ali", "age": 30}Output:
package main
type Root struct {
Name string `json:"name"`
Age int64 `json:"age"`
}इस converter के बारे में
Go के encoding/json को struct shapes declared चाहिए, और sample payload से उन्हें हाथ से लिखना ठीक वह झंझट है जिसे यह automate करता है। Field names PascalCased होते हैं (मूल key json tag में सुरक्षित, साथ में ,omitempty उन keys के लिए जो array input के कुछ items से missing हों)। Types Go conventions का पालन करते हैं: whole numbers int64, decimals float64, nested objects अलग named structs के रूप में extracted ताकि वे दोबारा उपयोग हो सकें। दो ईमानदार limits देखें: sample में whole आई संख्या (30) int64 type होती है भले ही API बाद में 30.5 भेजे — इसे manually widen करें; और single-letter या acronym keys (id, url) idiomatic ID/URL के बजाय Id/Url PascalCase होती हैं — आपके editor में तीस-second का rename।
अक्सर पूछे जाने वाले सवाल
int64 क्यों और int क्यों नहीं?
JSON numbers की कोई fixed width नहीं है; int64 वह safe default है जिसमें encoding/json whole values को 64-bit platforms पर बिना overflow के unmarshals करता है।
क्या यह mixed objects की arrays संभाल सकता है?
हाँ — keys items के पार merged होती हैं और missing keys को ,omitempty मिलता है, जैसे ragged API payloads behave करते हैं।