3

Here is my very simple C# class object that I am trying to turn into a JSON string and stringifying:

public class rTestObject
{
    public rTestObject()
    {
        Name = "Hello";
        State = 7;
    }

    public string Name { get; set; }
    public int State { get; set; }
}

I call the following static method:

    // Json.net
    // from Newtonsoft.Json.dll
    // version 8.0.2.19309
    //

    using Newtonsoft.Json;  
    public static string ConvertToJSON<T>(T obj)
    {
        return JsonConvert.SerializeObject(obj);
    }

Which produces the following (expected) output:

    {"Name":"Hello","State":7}

I call the following static method to stringify my JSON string

    public static string Stringify(string json)
    {
        return JsonConvert.ToString(json);
    }

Which produces the following (I think this is expected ??) output:

    "{\"Name\":\"Hello\",\"State\":7}"

My question how do I get from:

    "{\"Name\":\"Hello\",\"State\":7}"

back to rTestObject?

Here is what I have tried:

TRY 1

    public static T ConvertFromStringifiedJSON<T>(string stringifiedJSON)
    {
        Newtonsoft.Json.Linq.JObject j = Newtonsoft.Json.Linq.JObject.Parse(stringifiedJSON);
        return ConvertFromJSON<T>(j.ToString());
    }

Here is the exception:

    {"Error reading JObject from JsonReader. Current JsonReader item is not an object: String. Path '', line 1, position 34."}

TRY 2

    public static T ConvertFromJSON<T>(string jsonNotation)
    {
        return JsonConvert.DeserializeObject<T>(jsonNotation, NoNullStrings);
    }

Here is the exception:

    {"Error converting value \"{\"Name\":\"Hello\",\"State\":7}\" to type 'RestApi.Objects.rTestObject'. Path '', line 1, position 34."}

Solution:

Thank you Alexander I. !

    public static T ConvertFromStringifiedJSON<T>(string stringifiedJSON)
    {
        var json = JsonConvert.DeserializeObject<string>(stringifiedJSON);
        return ConvertFromJSON<T>(json);
    }
MrLister
  • 634
  • 7
  • 32
  • 4
    Have you tried `JsonConvert.DeserializeObject(str);` – ne1410s Dec 06 '17 at 20:35
  • Try decorating with Jason attributes https://stackoverflow.com/a/8796648/1585883 https://www.newtonsoft.com/json/help/html/T_Newtonsoft_Json_JsonPropertyAttribute.htm or use Datacontracts to get exactly what you want https://msdn.microsoft.com/en-us/library/jj870778.aspx – AllSpark Dec 06 '17 at 20:44
  • I don't think I need to add "Jason attributes" or "Datacontracts"... I believe this should be a pretty straight forward operation... but just don't know the Json.net api well enough to code it up... – MrLister Dec 06 '17 at 20:46

2 Answers2

4

Please review next example:

class Program
    {
        static void Main(string[] args)
        {
            var test = new TestObject
            {
                Name = "Hello",
                State = 7
            };

            var json = ConvertToJson(test);
            var stringify = Stringify(json);

            var result = StringifyToObject<TestObject>(stringify);
        }

        public static string ConvertToJson<T>(T obj)
        {
            return JsonConvert.SerializeObject(obj);
        }

        public static string Stringify(string json)
        {
            return JsonConvert.ToString(json);
        }

        public static T StringifyToObject<T>(string stringify)
        {
            var json = JsonConvert.DeserializeObject<string>(stringify);
            var result = JsonConvert.DeserializeObject<T>(json);
            return result;
        }
    }

    public class TestObject
    {
        public TestObject()
        {
            Name = "Hi";
            State = 0;
        }

        public string Name { get; set; }
        public int State { get; set; }
    }

Look at StringifyToObject method.

  1. Convert stringified string to JSON string
  2. Desirialize JSON string to object
Alexander I.
  • 2,380
  • 3
  • 17
  • 42
0

Like @ne1410 writes, use: JsonConvert.DeserializeObject<rTestObject>(myJsonString);

You don't have to stringify you Json, It is already a string as seen by the return type of JsonConvert.SerializeObject(obj)

You don't need any of the other methods IMO.

torsan
  • 401
  • 5
  • 14
  • I have to pass the JSON string as a string... (https://www.webucator.com/how-to/how-send-receive-json-data-from-the-server.cfm) so it cannot be pure JSON... unfortunately... I am sending the data to the server and then receiving it on another device... where the JSON MUST be a string... So I know this can be done... I just need to do it in C# ... not Javascript – MrLister Dec 06 '17 at 20:51
  • Ok, there is no such thing as 'pure JSON' in C#. It's either an object (Deserialized) or a string (Serialized). JsonConvert.Serialize(object) gives you your string. – torsan Dec 06 '17 at 21:01