【问题标题】:Show Json Data obtained from PHP File in Windows Phone 8.1在 Windows Phone 8.1 中显示从 PHP 文件获得的 Json 数据
【发布时间】:2015-09-19 21:14:45
【问题描述】:

我有 PHP 文件,它给了我以下 JSON:

{"Name":"Waqas","Age":37,"Address":"Kanju"}

当我在 Windows Phone 中执行这个方法时,它给了我相同的 JSON:

{"Name":"Waqas","Age":37,"Address":"Kanju"}

在名为tblock.Text的文本块中;

这是我从 PHP 文件中以 JSON 格式接收数据的方法:

public async void sndandrec(string feedingaddress, HttpResponseMessage   response, TextBlock tblock, HttpClient myhttpClient)
 string responseText;
            tblock.Text = "Waiting for response ...";
          try
        {
            response = await myhttpClient.GetAsync(resourceUri);
            response.EnsureSuccessStatusCode();
        responseText = await response.Content.ReadAsStringAsync();
           }
        catch (Exception ex)
        {
            // Need to convert int HResult to hex string
            tblock.Text = "Error = " + ex.HResult.ToString("X") +
                "  Message: " + ex.Message;
            responseText = "";
        }
       tblock.Text = response.StatusCode + " " + response.ReasonPhrase;

       tblock.Text = responseText.ToString();      

这是我的课:

public class RootObject
{
    public string Name { get; set; }
    public int Age { get; set; }
    public int Address { get; set; }
}

我想在TextboxName 中显示Name 值,类似于TextboxAge 中的Age 值和TextboxAddress 中的Address 值。我不知道该怎么做。

【问题讨论】:

  • 开始阅读here。

标签: c# php json xaml windows-phone-8.1


【解决方案1】:

好的,主要编辑,我基本上删除了我最后的所有答案,因为它不正确。

引用 JSON 库,最简单的方法是在 NuGet 上搜索 JSON.NET 并引用它。然后您可以调用您的服务器并解析 JSON 数据。

WebRequest request = WebRequest.Create("http://addresstojson.com/json.json");
WebResponse response = await request.GetResponseAsync();

using(var stream = new StreamReader(response.GetResponseStream()))
{
    json = JsonConvert.DeserializeObject<RootObject>(stream.ReadToEnd());
}

然后您仍然可以使用您在问题中定义的 RootObject 类检索到的数据来设置文本块

tbName.Text = "Name: " + json.Name;
tbAge.Text = "Age: " + json.Age;
tbAddress.Text = "Address: " + json.Address;

这是我用于此示例的 JSON:

{
    "name": "John Doe",
    "age": 25,
    "Address": "Mars"
}

【讨论】:

  • 使用 System.Web.Script.Serialization;在 Windows Phone 8.1 中无法识别,
  • 您是否像我提到的那样添加了对网络扩展的引用?
  • 我想我应该使用 DataContactJsonSerializer 这是在使用 System.Runtime.Serialization.Json;
  • 我从来没有为 WP8+ 开发过,所以我不知道组件是否在那里。我去搞个快应用看看
  • 如果你可以使用 System.Runtime.Serializtion.Json 做到这一点。那么请帮助我试过了,但语法错误`var jsonSerializer = new DataContractJsonSerializer(myCars,responseText);`
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多