-2

I have one application where some dummy data are kept :

 public class ValuesController : ApiController
{
    // GET api/values
    public string Get()
    {
        List<Cars> carslist = new List<Cars>();
        carslist.Add(new Cars { Id = "1", Color = "RED", CompanyName = "AUDI", Years = "1993", Model = "A4" });
        carslist.Add(new Cars { Id = "2", Color = "BLUE", CompanyName = "MERCEDES", Years = "1992", Model = "BENZ" });
        carslist.Add(new Cars { Id = "3", Color = "GREEN", CompanyName = "BMW", Years = "1990", Model = "C-CLASS" });
        carslist.Add(new Cars { Id = "4", Color = "YELLOW", CompanyName = "VOLKSWAGEN", Years = "1989", Model = "JETTA" });
        //return carslist;
        //JavaScriptSerializer jss = new JavaScriptSerializer();
        //string jsonstring = jss.Serialize(carslist);
        //return jsonstring;

        string JsonString = JsonConvert.SerializeObject(carslist);
        return JsonString;
    }

After JSON Formatting is done, i am requesting this particular data from another application, and Deserializing there, but it always showing error. The code from which i am accepting this data is below:

public ActionResult Index()
    {

        Uri url = new Uri(@"http://localhost:51933/api/Values");
        WebRequest webRequest = WebRequest.Create(url);
        WebResponse response = webRequest.GetResponse();
        StreamReader streamReader = new StreamReader(response.GetResponseStream());
        string responseData = streamReader.ReadToEnd().Trim();

        var sdf = JsonConvert.DeserializeObject<List<Cars>>(responseData);

        return View();
    }

The Model is like this

    public class Cars
{
    public string Id { get; set; }
    public string CompanyName { get; set; }
    public string Model { get; set; }
    public string Years { get; set; }
    public string Color { get; set; }

}

public class ListOfCars
{
    public List<Cars> listofcars { get; set; }
}

I am really not understanding, why the deserialization is not happening. Please help me out.

Thanks in advance.

Both the applications are running on localhost

This is the error :

Error

Arka
  • 985
  • 1
  • 8
  • 15
  • What error does it show up? – Radu Ungureanu Apr 02 '18 at 14:54
  • **JsonSerializationException** was unhandled by code – Arka Apr 02 '18 at 14:55
  • Error converting value "[{"Id":"1","CompanyName":"AUDI","Model":"A4","Years":"1993","Color":"RED"},{"Id":"2","CompanyName":"MERCEDES","Model":"BENZ","Years":"1992","Color":"BLUE"},{"Id":"3","CompanyName":"BMW","Model":"C-CLASS","Years":"1990","Color":"GREEN"},{"Id":"4","CompanyName":"VOLKSWAGEN","Model":"JETTA","Years":"1989","Color":"YELLOW"}]" to type 'System.Collections.Generic.List`1[MainApp.Models.Cars]'. Path '', line 1, position 404. – Arka Apr 02 '18 at 14:56
  • Works for me. Hmm. What's with the ListOfCars class? you don't seem to be using it anywhere – Radu Ungureanu Apr 02 '18 at 15:10
  • I just wrote it...thought of using...but never used it – Arka Apr 02 '18 at 15:20
  • 1
    Could you try `var sdf = JsonConvert.DeserializeObject(responseData);` ? – daylight Apr 02 '18 at 15:25
  • Its the same !! – Arka Apr 02 '18 at 16:58
  • Hello, and welcome to stackoverflow. Can you [edit] your question to include your errors as text (which you can get by picking **Copy exception detail to the clipboard**, then pasting it here) rather than as a screenshot? It's policy here not to to use images for this purpose, see [*Discourage screenshots of code and/or errors*](https://meta.stackoverflow.com/a/307500/3744182) and [*Why not upload images of code on SO when asking a question*](https://meta.stackoverflow.com/a/285557) for why. – dbc Apr 02 '18 at 18:18
  • Are you double-serializing your JSON? I.e. manually serializing it in your `Get()` method, after which the framework serializes it a second time? See [JSON.NET Parser *seems* to be double serializing my objects](https://stackoverflow.com/q/25559179/3744182) for a similar question. – dbc Apr 02 '18 at 18:21
  • First i am serializing it, then deserializing it – Arka Apr 05 '18 at 09:55

1 Answers1

0

From the screenshot with your error I can see that you are trying to deserialize to ListOfCars class, not to List<Cars>. Change the type to List<Cars>.

Radu Ungureanu
  • 131
  • 1
  • 9