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

0

2 Answers 2

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);
Sign up to request clarification or add additional context in comments.

3 Comments

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 I prefer the steps described in this post: stackoverflow.com/q/34043384/993547
@PatrickHofman You are right the classes generated by 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.
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.

7 Comments

That class layout is invalid for this JSON. I guess it is not your fault but that of Visual Studio.
Class is generated by VS. It isn't invalid :) Your can check it by yourself.
Can you paste your code for deserializing on pastebin.com ?
Not here. Class1[] Property is the problem.
Yes, List<Class1> instead of Rootobject. That is why your code if faulting.
|

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.