显示从Windows Phone 8.1中的PHP文件获得的Json数据 [英] Show Json Data obtained from PHP File in Windows Phone 8.1

查看:91
本文介绍了显示从Windows Phone 8.1中的PHP文件获得的Json数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个PHP文件,该文件为我提供了以下JSON:

I have PHP file which gives me following JSON:

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

当我在Windows Phone中执行此方法时,它会提供相同的JSON:

When I execute this method in Windows Phone it gives me the same JSON:

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

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

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

This is my method for receiving data from PHP file in JSON format:

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值.我不知道该怎么办.

I would like to show the Name value in TextboxName, similary Age value in TextboxAge and Address value in TextboxAddress. I don't know how to do that.

推荐答案

好的,主要修改,我基本上删除了所有我的最后答案,因为它是不正确的.

Okay, major edit, and I basically removed all of my last answer because of it being incorrect.

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

Reference a JSON library, the easiest is to search for JSON.NET on NuGet and reference that. Then you can make a call to your server and parse the JSON data.

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类检索的数据来设置文本块

Then you can still set the textblocks with the data retrieved using your RootObject class you defined in your question

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

这是我在此示例中使用的JSON:

Here is the JSON I used for this example:

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

这篇关于显示从Windows Phone 8.1中的PHP文件获得的Json数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆