duanguangsong2380
2015-09-19 21:14
浏览 66

显示从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.

图片转代码服务由CSDN问答提供 功能建议

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

   {“名称”:“Waqas”,“年龄”:37,“地址”:“Kanju”} 
   
 
 

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

  {“Name”:“Waqas”,“Age”:37,“Address”:“Kanju”} 
   
 
 

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

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

  public async void sndandrec(string feedingaddress,HttpResponseMessage response,TextBlock tblock,HttpClient myhttpClient)
 string responseText; 
 tblock.Text =“等待响应......”; \  n try 
 {
 response = await myhttpClient.GetAsync(resourceUri); 
 response.EnsureSuccessStatusCode(); 
 responseText = await response.Content.ReadAsStringAsync(); 
} 
 catch(Exception ex)
  {
 //需要将int HResult转换为十六进制字符串\  n 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; 组;  } 
 public int Age {get; 组;  } 
 public int Address {get; 组;  } 
} 
   
 
 

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

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

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"
    }
    
    点赞 打赏 评论

相关推荐 更多相似问题