0

I am requesting a JSON from a standard web service and I need to handle the response so I can work with the objects. I am working in Xamarin Studio - not that i think that matters.

You can see a result from web service by: https://dawa.aws.dk/vejnavne/autocomplete?q=due This is requesting street names in Denmark with 'due' in it.

    public async Task doAsyncAddress(string input)
    {
        var template = "https://dawa.aws.dk/vejnavne/autocomplete?q={0}";
        var url = string.Format(template, input);

        using (var httpClient = new HttpClient())
        {
            try
            {
                Task<HttpResponseMessage> getResponse = httpClient.GetAsync(url);

                HttpResponseMessage response = await getResponse;
                var responseJsonString = await response.Content.ReadAsStringAsync();

                /*

I have tried different things here, with with JsonConvert and JObject but neither works.. I have an idea that the son string is in wrong format, with "\n" included and i have tried to remove these, but still without results. I can see the string so I know it is there.. But it is not formatted correctly.

            */
            }
            catch (Exception ex)
            {
                string message = ex.Message;
                return message;
            }
        }

    }

With the JsonConverter.DeserializeObject i do this:

     var adress = JsonConvert.DeserializeObject<List<Address>>(responseJsonString);

where Address:

public class Address
{
    public string tekst { get; set; }
    public List<Vejnavne> vejnavn
    { get; set; }
}

public class Vejnavne
{
    public string href { get; set; }
    public string navn { get; set; }
}

and the response is:

"Cannot deserialize the current JSON object (e.g. {\"name\":\"value\"}) into type 'System.Collections.Generic.List`1[MinEjendom.Vejnavne]' because the type requires a JSON array (e.g. [1,2,3]) to deserialize correctly.\nTo fix this error either change the JSON to a JSON array (e.g. [1,2,3]) or change the deserialized type so that it is a normal .NET type (e.g. not a primitive type like integer, not a collection type like an array or List) that can be deserialized from a JSON object. JsonObjectAttribute can also be added to the type to force it to deserialize frenter code hereom a JSON object.\nPath '[0].vejnavn.href', line 5, position 11.”

And with JObject i get:

"Error reading JObject from JsonReader. Current JsonReader item is not an object: StartArray. Path '', line 1, position 1."

Bob Swager
  • 884
  • 9
  • 25
ako89
  • 57
  • 2
  • 11

2 Answers2

3

Your C# code is wrong. This is the correct one:

public class Vejnavn
{
    public string href { get; set; }
    public string navn { get; set; } // not List<Vejnavne> vejnavn
}

public class Address
{
    public string tekst { get; set; }
    public Vejnavn vejnavn { get; set; }
}

Then call it like this:

var adress = JsonConvert.DeserializeObject<List<Address>>(responseJsonString);
Patrick Hofman
  • 153,850
  • 22
  • 249
  • 325
  • 2
    Additional to this answer: Whenever you don't know how your class structure has to look like when yo want to parse a specific json string and you are working with Visual Studio, you can make use of its integrated "Paste Special" function. Just copy the regarding json string and go to Edit -> Paste Special -> Paste JSON As Classes. Visual Studio will create a suitable class structure for you. – croxy Jun 20 '17 at 11:36
  • 1
    @croxy I prefer the steps described in this post: https://stackoverflow.com/q/34043384/993547 – Patrick Hofman Jun 20 '17 at 11:39
  • @PatrickHofman You are right the classes generated by http://json2csharp.com/ are looking much cleaner than the output which Visual Studio gives with its "Paste Special" function. Funny thing: It seems like I even upvoted your answer in the past. I must have forgotten about this. Thanks for the reminder. – croxy Jun 20 '17 at 11:45
2

When you've JSON, you are .NET developer and finally - you have to convert JSON to C# class, you should use Edit - > Paste Special -> Paste JSON as classes. This is an awesome tool :)

Your code is wrong. This is the generated class from your JSON :

public class Class1
{
    public string tekst { get; set; }
    public Vejnavn vejnavn { get; set; }
}

public class Vejnavn
{
    public string href { get; set; }
    public string navn { get; set; }
}

When you have successfully generated your code, you can rename the class.

Bob Swager
  • 884
  • 9
  • 25