0

Currently in our project we have a certain class which has a String description.
Furthermore we have an object.

Object Foo { 
    Apple: {parts: 5},
    Description:  theproblematicobject,
    ,,,, 
}

Description is set up as a type string. Thus the object needs to be serialized to be assigned as the value of the description.

At the end we return foo as a JSONConvert.serializer(foo). Upon recieve, object foo is nicely formatted JSON, but the description is still a serialized string.

Is there a combinations of functions I can use how I can serialize the problematic object (so it fits the string type) and upon deserialization on request, it becomes one nicely formatted json instead of.

Result:

"Name": "Friday, 21 October 2016 New...",
  "Description": "{\"HoursList\":[],\"EmployeeFullname\":\"..........",
  "SwipeLeftAction": null,
  "SwipeLeftDescription": null,
  "SwipeLeftColor": null,
  "SwipeRightAction": null,
  "SwipeRightDescription": null,
  "SwipeRightColor": null,
  "ClickAction": "uiweb/em......."
}

Desired:

"Name": "Friday, 21 October 2016 New...",
  "Description": {"HoursList":[],
                  "EmployeeFullname" : ".........."
                 }
  "SwipeLeftAction": null,
  "SwipeLeftDescription": null,
  "SwipeLeftColor": null,
  "SwipeRightAction": null,
  "SwipeRightDescription": null,
  "SwipeRightColor": null,
  "ClickAction": "uiweb/em......."
}
CroMagnon
  • 1,218
  • 7
  • 20
  • 32

3 Answers3

0

The type of description is a string. You can clearly see that from the JSON itself. That means you get back a string from any automatic deserializer.

If you want an object out of it, run a second JSON deserializer pass yourself on the description field.

Blindy
  • 65,249
  • 10
  • 91
  • 131
-1

Probably, you have any formatter or something other logic that do the wrong serialization. What library version do you use?

The latest JsonConvert.SerializeObject works well.

Here is a little example:

private class Foo
{
    public int Apple { get; set; }
    public Description Description { get; set; }
}

private class Description
{
    public int[] HoursList { get; set; }
}


var a = new Foo
{
    Apple = 1,
    Description = new Description
    {
        HoursList = new[] {1}
    }
};

var b = JsonConvert.SerializeObject(a);

The result is a well formatted json string:

{"Apple":1,"Description":{"HoursList":[1]}}
Yuriy A.
  • 750
  • 3
  • 19
-1

Try this:

Serialize the problematic object to an XML string instead of JSON as shown here: Serialize an object to XML

Convert XML to JSON as shown here: How to convert JSON to XML or XML to JSON?

Community
  • 1
  • 1
C. Suttle
  • 170
  • 1
  • 11