【问题标题】:construct json Object in java and get value from JSONObject在java中构造json对象并从JSONObject中获取值
【发布时间】:2011-05-16 07:33:46
【问题描述】:

我是 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",  }
                 ]
}

【问题讨论】:

    标签: java javascript json


    【解决方案1】:

    小心。如果变量“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);
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-01-29
      • 1970-01-01
      • 2017-10-02
      相关资源
      最近更新 更多