I have a PHP
Web Service that provides a JSON
object. In my Windows Phone 8 Application, I consume the service by doing the following:
public void GetSigns()
{
var webClient = new WebClient();
var uri = new Uri("someURL");
webClient.OpenReadCompleted += GetSigns_Completed;
webClient.OpenReadAsync(uri);
}
private void GetSigns_Completed(object sender, OpenReadCompletedEventArgs e)
{
using (var sr = new StreamReader(e.Result))
{
var data = sr.ReadToEnd();
var result = JsonConvert.DeserializeObject<List<GetSignsResponse>>(data);
}
}
The data
object within the using statement is correctly populated with the JSON string. However, the result object throws an exception (I have removed the try and catch to make the code more readable).
This is what my GetSignsResponse
object looks like:
public class GetSignsResponse
{
public int ID { get; set; }
public string Online { get; set; }
public string Location { get; set; }
public DateTime Maxupdated { get; set; }
public string Line1 { get; set; }
public string Line2 { get; set; }
public string Line3 { get; set; }
public string Line4 { get; set; }
}
The JSON returns the following:
The Web Service returns a collection of the GetSignsResponse
object.
Any idea why the result
object within the GetSigns_Completed
callback method doesn't correctly work? Is my GetSignsResponse
object missing something?
The Error which I am getting is this that it fails to convert the JSON in to a List
of GetSignsResponse
.
Update The Web-Service JSON looks like this:
{
sign: {
id: "11",
online: "1",
location: "abc",
maxupdated: "2013-11-19 16:59:05",
line1: " abc ",
line2: " abc ",
line3: " abc ",
line4: " "
}
},
{
sign: {
id: "7",
online: "1",
location: "abc",
maxupdated: "2013-11-19 16:58:03",
line1: " abc ",
line2: " abc ",
line3: " abc ",
line4: " "
}
},
The data
object within the GetSigns_Completed
looks like this:
"\\t{\\"signs\\":[{\\"sign\\":{\\"id\\":\\"1\\",\\"online\\":\\"1\\",\\"location\\":\\"abc\\",\\"maxupdated\\":\\"2013-10-05 06:29:02\\",\\"line1\\":\\" \\",\\"line2\\":\\" \\",\\"line3\\":\\" \\",\\"line4\\":\\" \\"}},{\\"sign\\":{\\"id\\":\\"2\\",\\"online\\":\\"1\\",\\"location\\":\\"abc\\",\\"maxupdated\\":\\"2013-10-05 18:21:01\\",\\"line1\\":\\" abc \\",\\"line2\\":\\" abc \\",\\"line3\\":\\" abc \\",\\"line4\\":\\" \\"}},{\\"sign\\""}}]}"
The way in which the JSON was formed was incorrect in PHP. I had:
$signs[] = array($sign);
which needed to be:
$signs[] = $sign;
The latter is correct as it placed a sign object within the signs array and not a sign array within the sign's' array.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.