简体   繁体   English

如何从我的 json 对象中删除不必要的 \\\'?

[英]How to remove unnecessary \\u0027 from my json object?

I am trying to perform a simple convert operation on my string.我正在尝试对我的字符串执行简单的转换操作。 The objective is to convert my string to json format.目标是将我的字符串转换为 json 格式。

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:但是,当我打印 json 时..输出显示如下:

"{ clientID:\'YmY3NjZkM2ItZDg3MC00ZjQwLTk1NjktNTE0N2M1YzExOWMx\', clientSecret:\'ZDUyYTYyMjYtMTcyNC00ODkxLWI2NmMtMDlhZjA3MDdiOGQ2\'}\\" "{ clientID:\'YmY3NjZkM2ItZDg3MC00ZjQwLTk1NjktNTE0N2M1YzExOWMx\', clientSecret:\'ZDUyYTYyMjYtMTcyNC00ODkxLWI2NmMt3Mdlh0"

How do i remove unnecessary \' ?我如何删除不必要的 \' ?

First step is to model a class that matches the JSON structure you need.第一步是对一个与您需要的 JSON 结构匹配的类进行建模。 In this case a class with 2 string properties will do:在这种情况下,具有 2 个字符串属性的类将执行以下操作:

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:然后我们可以将其序列化为 JSON:

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

The result in json will be: json的结果将是:

{"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.尽管您使用JavaScriptSerializer的代码仍然可以工作,但文档中指出您最好切换到Json.NET来序列化 JSON。 Your approach will be the same.您的方法将是相同的。

Using JObject from Newtonsoft.JSON.Linq will address the issue for someone trying to convert string directly..使用 Newtonsoft.JSON.Linq 中的 JObject 将解决试图直接转换字符串的人的问题。

   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"
}

You can replace your json text string with this function that removes those characters:您可以取代你的JSON字符串功能,消除这些字符:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM