如何删除显示在提琴手从我的JSON对象转义字符 [英] How can I remove escape characters from my JSON object being displayed in Fiddler

查看:179
本文介绍了如何删除显示在提琴手从我的JSON对象转义字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想seriaize我的C#对象一个JSON对象没有最后的文本字符串包括转义字符。

I would like to seriaize my C# object to a JSON object without the final text string including escape characters.

下面的方法是通过REST风格的设计要求和返回时,通过小提琴手叫下面的JSON对象,但是我想删除反斜线的,因此只包括双引号和尊重JSON格式

The below method is being called through a RESTful design and returns the following JSON object when called through fiddler, however I would like to remove the backslash's so it includes only the double quotes and respects the JSON format

{\model\:\Faslev\,\platform\:\ABC\,\year\:2010年,\month\\ \\:\June\,\plant\:\ONDH\,\country\:\Brazil\}

    public string GetModelBySerialNumber(string serialNumber)
    {
        var model = new Model();
        using (var connection = new SqlConnection(DBUtility.DbConnection))
        {
            try
            {                    
                SqlCommand myProcedure = new SqlCommand("myProcedure", connection);
                myProcedure.CommandType = CommandType.StoredProcedure;
                myProcedure.Parameters.Add("@SerialNumber", SqlDbType.NVarChar).Value = serialNumber;
                connection.Open();
                SqlDataReader dataReader = myProcedure.ExecuteReader();
                while (dataReader.Read())
                {
                    Func<int, string> GetString =  (int i) => dataReader.GetString(i);
                    Func<int, Int32> GetInteger = (int i) => dataReader.GetInt32(i);
                    model.ModelName = GetString(0);
                    model.Platform = GetString(1);
                    model.Year = GetInteger(2);
                    model.Month = GetString(3);
                    model.Plant = GetString(4);
                    model.Country = GetString(5);                                                
                }                    
            }
            catch (SqlException exception) {Trace.WriteLine("Error Trace " + exception.Message);}
            finally {connection.Close();}
        }
        return JsonConvert.SerializeObject(model);
    }

如果我用concatanation,如下图所示,那么oject正确显示没有反斜杠,但我真的不希望这样做,因为它似乎是一个太过复杂的方式写的对象了。

If I use concatanation, like below, then the oject displays correctly without the backslash, but I dont really want to do this as it seems an over complicated way to write an object out.

    public string Ping()
    {
        return "{Message" + ":" + "PONG" + "}";
    }



{消息:PONG}

推荐答案

如果您正在使用Web API,那么你就需要调用 JsonConvert.SerializeObject()。更改方法的返回类型为模式,然后简单地返回模型。网页API将系列化给你。

If you are using Web API, then you don't need to call JsonConvert.SerializeObject(). Change the return type of your method to Model and then simply return the model. Web API will serialize it for you.

这篇关于如何删除显示在提琴手从我的JSON对象转义字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆