bill bill - 5 months ago 27
JSON Question

how to convert a string with equal "=" sign to json in c#

I have following string, this returns from the web service

"Status=Success,PNR=76UUEI78787870,Customer_Ref=89511133545"


I want to convert this in to json like below

{
"Status": "Success",
"PNR": "76UUEI78787870",
"Customer_Ref": "89511133545"
}


how can I do that.

Answer
var str = "Status=Success,PNR=76UUEI78787870,Customer_Ref=89511133545";
var vals = str.Split(',');
var json = "{" +
            string.Join(",",
                vals.Select(val => val.Split('=')).Select(s => string.Format("\"{0}\": \"{1}\"", s[0], s[1]))) +
            "}";