在java中构造json对象并从JSONObject中获取值
我是 JSON
1 的新手。 我在 html 中得到了以下格式的 json
结果 JSon 结果
Alert(result)
{"resort0":"Abaco Beach Resort at Boat Harbour","resort1":"Alexandra Resort","room0":"1 Bedroom Luxury Oceanfront Suite","room1":"2 Bedroom Deluxe Ocean View Suite","room2":"Deluxe Garden View Studio","room3":"Deluxe Ocean View Studio","room4":"Deluxe Oceanfront","room5":"Oceanfront","room6":"Superior Oceanfront"}
alert(result.resort1); // alert "undefined"
alert(result.resort0); // alert "undefined"
2 。我如何使用java代码JSONObject获得这种格式 度假村是地图的关键吗?
{
"Resorts" : [
{ "name" : "Resort1", // First element
"room1" : "rooms1"
"room2" : "rooms2" },
{ "name" : "Resort2", // Second element
"room1" : "rooms1",
"room2" : "rooms2", }
]
}
i'm new to JSON
1. i got json result in html in following format
JSon Result
Alert(result)
{"resort0":"Abaco Beach Resort at Boat Harbour","resort1":"Alexandra Resort","room0":"1 Bedroom Luxury Oceanfront Suite","room1":"2 Bedroom Deluxe Ocean View Suite","room2":"Deluxe Garden View Studio","room3":"Deluxe Ocean View Studio","room4":"Deluxe Oceanfront","room5":"Oceanfront","room6":"Superior Oceanfront"}
alert(result.resort1); // alert "undefined"
alert(result.resort0); // alert "undefined"
2
. how do i get such format with java code JSONObject
is Resorts is key of map ?
{
"Resorts" : [
{ "name" : "Resort1", // First element
"room1" : "rooms1"
"room2" : "rooms2" },
{ "name" : "Resort2", // Second element
"room1" : "rooms1",
"room2" : "rooms2", }
]
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当心。如果变量“result”的 json 位于第二个代码块中,则不能指望使用“result.resort0”或“result.resort1”找到任何数据。在您的示例中,结果包含一个名为“Resorts”的子成员,它包含一个子成员数组。
换句话说,要循环遍历所有值,我希望 JavaScript 如下:
Be careful. If the json to the variable "result" is in your second code block, you can't expect to find any data by using "result.resort0" or "result.resort1". In your example, result contains a submember called "Resorts" which holds an array of submembers.
In other words, to cycle through all values, I would expect javascript like: