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