Show Json Data obtained from PHP File in Windows Phone 8.1

∥☆過路亽.° 提交于 2019-12-25 06:37:29

问题


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:


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"
}


来源:https://stackoverflow.com/questions/32673314/show-json-data-obtained-from-php-file-in-windows-phone-8-1

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!