4

I am working on 2 web applications; A & B. now i have a shared class named CRUDOutput as follow on both web applications:-

public class CRUDOutput
{
    public Operation4 operation { get; set; }
}
public class Operation4
{
    public Result result { get; set; }
    public string name { get; set; }
}
public class Result
{
    public string status { get; set; }
    public string message { get; set; }

}

now inside web application A i am returning the following:-

[HttpPost]
public ActionResult CreateResource(CreateResource cr)
{
    List<CRUDOutput> co = new List<CRUDOutput>();
            co.Add(JsonConvert.DeserializeObject<CRUDOutput>(crudoutput));
            co.Add(JsonConvert.DeserializeObject<CRUDOutput>(crudoutput2));

    return Json(JsonConvert.SerializeObject(co));
}

now from web application B, i am calling the action method as follow:-

try
{
    using (WebClient wc = new WebClient()) 
    {
        string url = "https://localhost:44302/" + "Home/CreateResource";
        Uri uri = new Uri(url);
        wc.Headers.Add(HttpRequestHeader.ContentType, "application/json");

        output = wc.UploadString(uri,  data);
    }
}
catch (WebException e)
{
}
List<CRUDOutput> result = JsonConvert.DeserializeObject<List< CRUDOutput>>(output);

but i will get the following exception when i tried to deserialize the output:-

Error converting value "[{"operation":{"result":{"status":"Success","message":"Resource has been added successfully to ......"},"name":"CREATE RESOURCE"}},{"operation":{"result":{"status":"Failed","message":"Account addition "},"name":"ADD ACCOUNTS"}}]" to type 'System.Collections.Generic.List`1[S.ViewModels.CRUDOutput]'. Path '', line 1, position 464.

now the JSON return from web application A will be as follow:-

"\"[{\\\"operation\\\":{\\\"result\\\":{\\\"status\\\":\\\"Success\\\",\\\"message\\\":\\\"Resource 123 rfrf has been added successfully \\\"},\\\"name\\\":\\\"CREATE RESOURCE\\\"}},{\\\"operation\\\":{\\\"result\\\":{\\\"status\\\":\\\"Failed\\\",\\\"message\\\":\\\"Account addition \\\"},\\\"name\\\":\\\"ADD ACCOUNTS\\\"}}]\""

so can anyone advice why i am unable to deserialize to a list of objects?

2
  • what is result in the controller method? Commented Jun 8, 2016 at 12:58
  • @AmitKumarGhosh sorry i miss type it ,, Commented Jun 8, 2016 at 13:09

1 Answer 1

7

The output as you've pasted is encoded as JSON twice. Compare the difference between:

"\"[{\\\"operation\\\":{\\\"result\\\":{\\\"status\\\":\\\"Success\\\",\\\"message\\\":\\\"Resource 123 rfrf has been added successfully \\\"},\\\"name\\\":\\\"CREATE RESOURCE\\\"}},{\\\"operation\\\":{\\\"result\\\":{\\\"status\\\":\\\"Failed\\\",\\\"message\\\":\\\"Account addition \\\"},\\\"name\\\":\\\"ADD ACCOUNTS\\\"}}]\""

and

"[{\"operation\":{\"result\":{\"status\":\"Success\",\"message\":\"Resource 123 rfrf has been added successfully \"},\"name\":\"CREATE RESOURCE\"}},{\"operation\":{\"result\":{\"status\":\"Failed\",\"message\":\"Account addition \"},\"name\":\"ADD ACCOUNTS\"}}]"

This happens because you're encoding the result as Json twice. Replace:

return Json(JsonConvert.SerializeObject(result));

with

return Json(result);    // This encodes as JSON automatically
Sign up to request clarification or add additional context in comments.

9 Comments

so you mean MVC will do the serialization for me ?
Yes, it will. The Json method takes an object, not a string.
so will there be any benefit of using JSON.net ? as seems mvc will do the job out of the box? or JSON.net will do things which MVC can not do out of the box ?
MVC uses json.net under the covers.
@johng If you want to use JavaScriptSerializer, then just use Json(result) as Richard showed you in his answer above.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.