2

I am trying to perform a simple convert operation on my string. The objective is to convert my string to json format.

string clientID= newClient.GetClientId();
string clientSecret = "ZDUyYTYyMjYtMTcyNC00ODkxLWI2NmMtMDlhZjA3MDdiOGQ2";
var stringObject= "{ clientID:"+ "'"+clientID+"', clientSecret:"+"'"+clientSecret+"'}" ;
var json= new JavaScriptSerializer().Serialize(stringObject);

However when i print the json ..The output shows something like this:

"{ clientID:\u0027YmY3NjZkM2ItZDg3MC00ZjQwLTk1NjktNTE0N2M1YzExOWMx\u0027, clientSecret:\u0027ZDUyYTYyMjYtMTcyNC00ODkxLWI2NmMtMDlhZjA3MDdiOGQ2\u0027}\"

How do i remove unnecessary \u0027 ?

the_coder_in_me
  • 143
  • 3
  • 15
  • 2
    Just make a class Foo with 2 string properties: clientID and clientSecret, assign the values to an instance of Foo and then serialize that instance of Foo, instead of serializing a string – rene Oct 31 '17 at 19:54
  • JSON uses double quotes, not single quotes. Plus, clientID and clientSecret should be encased in double quotes as well. – Josh Schultz Oct 31 '17 at 19:54
  • 1
    \u0027 is an apostrophe character you are adding yourself to the string. Use a proper JSON serialize like JSON.NET and serialize the class like @rene said. – Sam Marion Oct 31 '17 at 20:08

3 Answers3

2

You can replace your json text string with this function that removes those characters:

var json= new JavaScriptSerializer().Serialize(stringObject);
json = System.Text.RegularExpressions.Regex.Unescape(json);
1

First step is to model a class that matches the JSON structure you need. In this case a class with 2 string properties will do:

public class FooType 
{
   public string clientID {get;set;}
   public string clientSecret {get;set;}
}

Next we need to instantiate this class and set its properties:

var foo = new FooType { 
    clientID = newClient.GetClientId(),
    clientSecret = "ZDUyYTYyMjYtMTcyNC00ODkxLWI2NmMtMDlhZjA3MDdiOGQ2",
};

then we can Serialize that into JSON:

var json= new JavaScriptSerializer().Serialize(foo);

The result in json will be:

{"clientID":"42","clientSecret":"ZDUyYTYyMjYtMTcyNC00ODkxLWI2NmMtMDlhZjA3MDdiOGQ2"}

Notice how the values are enclosed in double quotes, not in single ones and that no extra care is needed for escaping, removing or replacing chars. That is all handled by serializers because that is their main responsibility.

Although your code using the JavaScriptSerializer will still work, it is stated in the documentation that you better switch to Json.NET for serializing JSON. Your approach will be the same.

rene
  • 41,474
  • 78
  • 114
  • 152
  • Thanks..I applied this and it works.. However, I was still wondering if there is a method to convert the string directly into json format. – the_coder_in_me Oct 31 '17 at 22:20
  • @the_coder_in_me you would have to write your own serializer logic then. The problems you had with your approach are only the tip of the iceberg of the issues you will face when writing your own serializer logic, assuming you want a specification compliant output: http://www.json.org/ – rene Nov 01 '17 at 07:03
0

Using JObject from Newtonsoft.JSON.Linq will address the issue for someone trying to convert string directly..

   JObject json = JObject.Parse(str);

This is the sample code:

using System.Web;
using Newtonsoft.Json.Linq;

namespace MyTestProjectConsole
{
    public class Test1Json
    {
        public void ConvertJson()
       {
            string clientID = "aa";
            string clientSecret = "bb";
            var stringObject = "{ clientID:" + "'" + clientID + "', clientSecret:" + "'" + clientSecret + "'}";
            var json = JObject.Parse(stringObject);
            Console.WriteLine(json);
        }

        public static void Main()
        {
            Test1Json obj = new Test1Json();
            obj.ConvertJson();
            Console.ReadKey();
        }
    }
}

This generates the following output in console:

{
  "clientID": "aa",
  "clientSecret": "bb"
}
the_coder_in_me
  • 143
  • 3
  • 15