2013-04-08 71 views
0

我目前正在研究一個地圖函數,它要求我顯示提取的JSON響應中的數據。這是我從JSON響應中獲得的類的一部分。我希望從JSON響應中提取「文本」以顯示在列表框中。將JSON響應中的數據綁定到列表框

public class Attributes 
    { 
     public double length { get; set; } 
     public double time { get; set; } 
     public string text { get; set; } //Want to display in listbox 
     public long ETA { get; set; } 
     public string maneuverType { get; set; } 
    } 

    public class rootobject 
    { 
     public Attribute attributes { get; set; } 
     public string compressedGeometry { get; set; } 
    } 

我試過在線學習,但示例中的數據都是硬編碼的。我的意思例如硬編碼:

enter image description here

任何幫助將不勝感激。

回答

0

以下是WS Web服務中的示例方法。

 [WebMethod] 
    public rootobject GetData() 
    { 
     var rootObj = new rootobject() 
     { 
      attributes = new Attribute[2] { new Attribute() { text = "text 1" }, new Attribute() { text = "text 2" } }, 
      compressedGeometry = "geometry 1" 
     }; 

     return rootObj; 
    } 

JavaScript代碼從服務提取數據

var webMethod = "WS.asmx/GetData"; 
var parameters = ""; 

$.ajax({ 
type: "POST", 
url: webMethod, 
data: parameters, 
contentType: "application/json; charset=utf-8", 
dataType: "json", 
success: function (msg) { 
    $("#sel").html(msg.d); 
    var index = 0; 
    for (; index < msg.d.attributes.length; index++) 
    { 
     $("#sel").append("<option>" + msg.d.attributes[index].text + "<option>"); 
    } 
}, 
error: function (e) { 
    alert(e); 
} 
}); 

HTML的下拉/選擇

<select id="sel"></select> 

+0

你的類可能有一個錯字的錯誤 - 當你已經在你的rootObject定義中聲明瞭「Attribute」 - wh ich似乎並不正確。您可以將屬性類名更改爲屬性,上面的代碼將爲您工作。 – 2013-04-08 02:00:10

+0

Thx爲我指定我的錯字。但我面臨的問題是,我有我的JSON響應,我想從json響應中提取「文本」以顯示在列表框中。 – qU3st 2013-04-08 02:35:41

+0

P.S JSON響應來自當前位置和最終位置。 – qU3st 2013-04-08 02:36:36