में लिपटे ऑब्जेक्ट्स पर रूबी to_json मैं एक साइड-प्रोजेक्ट के लिए एक सुपर आसान JSON webservice बनाने की कोशिश कर रहा हूं। हालांकि मुझे अपनी ऑब्जेक्ट्स जेएसओएन में परिवर्तित करने में कुछ परेशानी हो रही है, क्या कोई मेरी मदद कर सकता है?उद्धरण के
मैं निम्नलिखित श्रेणियां होती हैं:
class Location
attr_reader :street, :city, :state, :country, :zip, :latitude, :longitude
def initialize(street, city, state, country, zip, latitude, longitude)
@street = street
@city = city
@state = state
@country = country
@zip = zip
@latitude = latitude
@longitude = longitude
end
def to_json
{
'street' => @street,
'city' => @city,
'state' => @state,
'country' => @country,
'zip' => @zip,
'latitude' => Float(@latitude),
'longitude' => Float(@longitude)
}.to_json
end
end
और
class Spot
attr_reader :name, :category, :location, :id
def initialize(id, name, category, location)
@name = name
@category = category
@location = location
@id = id
end
def to_json
{
'name' => @name,
'category' => @category,
'location' => @location.to_json,
'id' => @id
}.to_json
end
end
एक यादृच्छिक इनपुट मैं चाहूँगा उत्पादन कुछ इस तरह होना करने के लिए दिया गया है:
{
"name":"Wirelab",
"category":"Bier",
"location":
{
"street":"Blaatstraat 12",
"city":"Enschede",
"state":"Overijssel",
"country":"Nederland",
"zip":"7542AB",
"latitude": 31.21312,
"longitude":41.1209
}
,
"id":"12"
}
हालांकि ouput मुझे यह मिल जाएगा:
{
"name":"Wirelab",
"category":"Bier",
"location":"
{
"street\":"Blaatstraat 12",
"city\":\"Enschede\",
\"state\":\"Overijssel\",
\"country\":\"Nederland\",
\"zip\":\"7542AB\",
\"latitude\":31.21312,
\"longitude\":41.1209
}
",
"id":"12"
}
क्या कोई मुझे बता सकता है कि मैं इसे कैसे ठीक कर सकता हूं?
संपादित करें:
मैं एक Sintra वेब सेवा जो कुछ इस तरह दिखता उपयोग कर रहा हूँ:
get '/spots' do
#json = spots.to_json
spot = Spot.new("12", "Wirelab", "Bier", Location.new("Blaatstraat 12", "Enschede", "Overijssel", "Nederland", "7542AB", "31.21312", "41.1209"))
json = spot.to_json
if callback
content_type :js
response = "#{callback}(#{json})"
else
content_type :json
response = json
end
response
end
आप कैसे करते हैं आउटपुट प्राप्त करें? क्या यह रखता है या निरीक्षण करता है? (आईआरबी में डिफ़ॉल्ट)। आपके उत्तर के लिए – thekindofme