2

I am trying to return a JSON result from a REST API that includes a JsonObject as an element.

var aJsonObject = new JObject();
aJsonObject.Add("somefield", "somevalue" );
aJsonObject.Add("someotherfield", 1995);

return Json( new { status = "success", result = aJsonObject } );

The client receives an empty nested arrays:

{"status":"success","result":[[[]],[[]]]}

My work around, which I don't love, is to serialize the JsonObject, thus sending it as a string and then having the client parse it. It works, but it's a bit ugly.

Is this a bug or am I doing it wrong?

NOTE: 8/3/18 I edited the variable declaration to correct a typo - it was jsonObject and should have been aJsonObject

6
  • 1
    Your example you gave, and your result, don't match up at all. Commented Aug 2, 2018 at 22:13
  • 2
    Why do you want to return a JObject rather then an object? Commented Aug 2, 2018 at 22:14
  • Why are you declaring jsonObject but populating and returning aJsonObject? Are you simply mixing up your variables? Commented Aug 2, 2018 at 22:33
  • @ozum.e I need it to be dynamic - I won't know the fields I am adding to the object until run time so I cannot declare a class to make an POCO from. Commented Aug 3, 2018 at 18:53
  • @DanWilson Sorry - that was typo... I renamed objects and variables for the purpose of posting the question to avoid publishing real code. I corrected the post and made a note that I edited it. Commented Aug 3, 2018 at 18:54

3 Answers 3

5

JObject is already json-formatted. Main purpose of JsonResult is to serialize an object to json. What you are trying to do is (I guess):

dynamic resultObject = new ExpandoObject();
resultObject.somefield = "somevalue";
resultObject.someotherfield = 1995;

return Json( new { status = "success", result = resultObject } );

If you want to build the Json string yourself and return it to the client you can use Content:

return new Content(yourjsonstring, "application/json");
Sign up to request clarification or add additional context in comments.

2 Comments

In my case, the object to serialize to Json, contains a property that has a type of JObject. When this happens, return Json(myObject) will blank out that property's json value and return the empty array like Daniel C showed in his original post - where it should just pass it on in my opinion. The other non-json properties serialize just fine. Feels like a bug to me.
Ig1382, how did you solve what you describe? I'm having the same issue...help!
2

And if you want to keep using JObject, this works (and then return the JSON as @ozum.e describes):

var jObject = new JObject();
jObject.Add("someField", "someValue");
jObject.Add("otherField", 1995);
var newObj = new { status = "success", result = jObject };
var returnThis = JsonConvert.SerializeObject(newObj);

Comments

0

You need to cast. It's because JObject is not compatible with the serializer used. But hours of struggle you can get MVC JsonResult serialize aJsonObject.ToObject<Dictionary<string, object>>() if it is a JObject and if it's a JArray you need aJsonObject.ToObject<List<Dictionary<string, object>>>()

Comments

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.