在java中构造json对象并从JSONObject中获取值

发布于 2024-11-08 03:38:58 字数 1003 浏览 0 评论 0原文

我是 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

不忘初心 2024-11-15 03:38:58

当心。如果变量“result”的 json 位于第二个代码块中,则不能指望使用“result.resort0”或“result.resort1”找到任何数据。在您的示例中,结果包含一个名为“Resorts”的子成员,它包含一个子成员数组。

换句话说,要循环遍历所有值,我希望 JavaScript 如下:

for(var i=0; i<result.Resorts.length; i++) {
  alert(result.Resorts[i].name);
  alert(result.Resorts[i].room1);
  alert(result.Resorts[i].room2);
}

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:

for(var i=0; i<result.Resorts.length; i++) {
  alert(result.Resorts[i].name);
  alert(result.Resorts[i].room1);
  alert(result.Resorts[i].room2);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文