2015-10-12 42 views
0

我想在Visual Basic 2013中的數據綁定應用程序的url中添加json,我在其中工作.....但我找不到任何方式,我怎麼能做這個。如何在Windows 8應用程序的數據綁定應用程序中添加json從數據綁定應用程序中的json

。我想從json url
添加數據。第二我想也顯示圖像
。左側的圖像和右側的文字。

JSON樣本:

{"worldpopulation":[{"titlejson":"Bullish trend observed in KSE with 300 points increase","titledesc":25695,"flag":""}]} 

我如何獲取JSON到Windows Phone

我想在這個頁面MainViewmodel.cs加載數據

public void LoadData() 
     { 
      // Sample data; replace with real data 
      this.Items.Add(new ItemViewModel() { ID = "0", LineOne = "runtime one", LineTwo = "Maecenas praesent accumsan bibendum", LineThree = "Facilisi faucibus habitant inceptos interdum lobortis nascetur pharetra placerat pulvinar sagittis senectus sociosqu" }); 

      this.Items.Add(new ItemViewModel() { ID = "0", LineOne = "runtime one", LineTwo = "Maecenas praesent accumsan bibendum", LineThree = "Facilisi faucibus habitant inceptos interdum lobortis nascetur pharetra placerat pulvinar sagittis senectus sociosqu" }); 

      this.IsDataLoaded = true; 
     } 
+0

你們是不是要消耗的服務? – Eldho

+0

是來自url的json服務 – user3273547

回答

0

你需要有這個模型適合你的json結果。

的模型對應於您的JSON結果

您可以使用json2csharp

public class Worldpopulation 
{ 
    public string titlejson { get; set; } 
    public int titledesc { get; set; } 
    public string flag { get; set; } 
} 

public class RootObject 
{ 
    public List<Worldpopulation> worldpopulation { get; set; } 
} 

您的視圖模型加載數據的方法應該調用使用HttpClient讓你json結果的web服務生成模型。它將deseralize到模型,那麼你可以傳送到您的視圖模型

LoadData()方法數據消費服務

using (HttpClient client = new HttpClient()) 
      { 

       client.BaseAddress = new Uri("http://api.openweathermap.org"); 

       var url = "data/2.5/forecast/daily?q={0}&mode=json&units=metric&cnt=7"; 

       client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); 

       HttpResponseMessage response = await client.GetAsync(url); 

       if (response.IsSuccessStatusCode) 
       { 
        var data = response.Content.ReadAsStringAsync(); 
        var yourResult= JsonConvert.DeserializeObject<RootObject>(data.Result.ToString()); 
       } 
      //Code to map to your view-model object and bind them to view 

      } 
+0

不能理解有兩個文件一個是main.xaml,另一個是mainviewmodel.cs,我可以在這裏編寫代碼 – user3273547

+0

數據就像使用System.Runtime.Serialization的視圖模型那樣; 命名空間PhoneApp1.Models { [DataContract] 公共類城市 { [數據成員(名稱爲 「名」)] 公共字符串名稱{;組; } [DataMember(Name =「countrycode」)] public string CountryCode {get;組; } [DataMember(Name =「population」)] public int Population {get;組; } } } – user3273547

相關問題