duanguangsong2380 2015-09-19 21:14
浏览 76
已采纳

显示从Windows Phone 8.1中的PHP文件获取的Json数据

I have PHP file which gives me following JSON:

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

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

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

in textblock named tblock.Text;

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();      

This is my class:

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

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.

  • 写回答

1条回答 默认 最新

  • dongxieting9623 2015-09-19 22:33
    关注

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

    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());
    }
    

    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;
    

    Here is the JSON I used for this example:

    {
        "name": "John Doe",
        "age": 25,
        "Address": "Mars"
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作