1

I want to convert this JSON return from php file into c#. But as a newbie don't know how, please help.

{"response": [
    {"user_id":"26","crtloc_lat":"47.678238","crtloc_lng":"-122.131416"},
    {"user_id":"27","crtloc_lat":"9.350192","crtloc_lng":"-95.391006"},
    {"user_id":"28","crtloc_lat":"47.678238","crtloc_lng":"-122.131416"}
]}

The above JSON is returned from my PHP file.

WebRequest request = WebRequest.Create("http://localhost/abh/returntest.php");
WebResponse response = await request.GetResponseAsync();
List<Response>  json ;
using (var stream = new StreamReader(response.GetResponseStream()))
{
    json = JsonConvert.DeserializeObject<List<Response>>(stream.ReadToEnd());
}
foreach (var item in json)
{
    Console.WriteLine("id={0},latitude={1},longitude={2}",item.user_id,item.crtloc_lat,item.crtloc_lng);
}            

The Classes I am using are:

public class Response
{
    public string user_id { get; set; }
    public string crtloc_lat { get; set; }
    public string crtloc_lng { get; set; }
}

public class RootObject
{
    public List<Response> response { get; set; }
}

I am getting this error:

Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'System.Collections.Generic.List`1[afterall.Response]' because the type requires a JSON array (e.g. [1,2,3]) to deserialize correctly.

1

3 Answers 3

3

The following line is wrong based on your sample JSON.

json = JsonConvert.DeserializeObject<List<Response>>(stream.ReadToEnd());

Try changing it to:

json = JsonConvert.DeserializeObject<RootObject>(stream.ReadToEnd());

Edit:

WebRequest request = WebRequest.Create("http://localhost/abh/returntest.php");
WebResponse response = await request.GetResponseAsync();
RootObject json;
using (var stream = new StreamReader(response.GetResponseStream()))
{
    json = JsonConvert.DeserializeObject<RootObject>(stream.ReadToEnd());
}
foreach (var item in json)
{
    Console.WriteLine("id={0},latitude={1},longitude={2}",item.user_id,item.crtloc_lat,item.crtloc_lng);
}   
Sign up to request clarification or add additional context in comments.

3 Comments

@WaqasKanju: Dietz is correct. This fixes your problem. Tested with you code and gave in the string you posted.
@WaqasKanju you have to change the declaration of the json variable aswell.
Foreach will not work after this edit: You have to use this Console.WriteLine(json.response[0].user_id); Console.WriteLine(json.response[1].user_id); Console.WriteLine(json.response[2].user_id);
0

rewrite the following line :

json = JsonConvert.DeserializeObject<List<Response>>(stream.ReadToEnd());

as

json = JsonConvert.DeserializeObject<Response>(stream.ReadToEnd());

1 Comment

After changing that line: it shows this error, can not implicitly convert type RootObject to System.Collection.Generics.List
0

From your testlink in the comment of your post. Your response is not:

{"response": [
    {"user_id":"26","crtloc_lat":"47.678238","crtloc_lng":"-122.131416"},
    {"user_id":"27","crtloc_lat":"9.350192","crtloc_lng":"-95.391006"},
    {"user_id":"28","crtloc_lat":"47.678238","crtloc_lng":"-122.131416"}
]}

as you post but rather:

connected{"response": [
         {"user_id":"26","crtloc_lat":"47.678238","crtloc_lng":"-122.131416"},
          {"user_id":"27","crtloc_lat":"9.350192","crtloc_lng":"-95.391006"},
          {"user_id":"28","crtloc_lat":"47.678238","crtloc_lng":"-122.131416"
          }]
    }

This is not corresponding to your sample code. Remove the

connected{ }

wrapping or return correct json for a connected object (whatever the connect is). If you remove the wrapping it should work as @Dietz posted.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.