简体   繁体   中英

exception when deserializing json object

I am attempting to deserialize some simple json into the below objects

    public class Car
    {
        public int car_id { get; set; }
        public string name { get; set; }
    }

    public class RootObject
    {
        public List<Car> cars { get; set; }
    }

This is the call I make

 using (var client = new HttpClient(handler))
                {

                    client.BaseAddress = new Uri("http://localhost/WebApiServer/Reference/");

                    client.DefaultRequestHeaders.Accept.Clear();
                    client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));


                    HttpResponseMessage response = client.GetAsync("cars").Result;
                    if (response.IsSuccessStatusCode)
                    {
                        HttpContent httpContent = response.Content;
                        string responseString = httpContent.ReadAsStringAsync().Result;
//error happens here
                        var deserializeObject = JsonConvert.DeserializeObject<RootObject>(responseString);
                    }

this is the value of response string from debugger in VS

"\"{ \\\"cars\\\": [{\\\"car_id\\\":46,\\\"name\\\":\\\"Ford-Fiesta\\\"]}}\""

and this is the exception

//exception

 Error converting value "{ "cars": [{"car_id":46,"name":"Ford-Fiesta"]}}" to type 'WebApiJson.Program+RootObject'. Path '', line 1, position 62.

this is the json - I am reading it from a file

 { "cars": [{"car_id":46,"name":"Ford-Fiesta"}]}

I have run out of ideas

EDIT:

I just tried

 string serializeObject = JsonConvert.SerializeObject(text);
    var deserializeObject = JsonConvert.DeserializeObject<RootObject>(serializeObject); 

and it is giving me the same problem

EDIT 2 my controller returning the json

 public string Cars()
        {
            string text = System.IO.File.ReadAllText("json.txt");

            string serializeObject = JsonConvert.SerializeObject(text);

            Debug.WriteLine(serializeObject);
//            this fails
            var deserializeObject = JsonConvert.DeserializeObject<RootObject>(serializeObject);

            return JsonConvert.SerializeObject(text);
        }
              string text = System.IO.File.ReadAllText("json.txt");

                //this line is WRONG!!
                string serializeObject = JsonConvert.SerializeObject(text);

                //this fails because serializeObject is a double serialized string
               //which you try to deserialize to RootOject                
                 var deserializeObject = JsonConvert.DeserializeObject<RootObject>(serializeObject);

                    return JsonConvert.SerializeObject(text);

You are serializing string into a string.

 string serializeObject = JsonConvert.SerializeObject(text);

This is wrong, you should be deserializing to an object.

RootObject obj = JsonConvert.DeserializeObject<RootObject>(text);

This is why your Deserialize fails, because you serialize your initial json into a second string, then try to deserialize it into a RootObject.

Your controller should read something like this

  public RootObject Cars()
            {
                string text = System.IO.File.ReadAllText("json.txt");


                RootObject serializeObject = JsonConvert.DeserializeObject<RootObject>(text);

              return serializeObject;
            }

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM