如何从嵌套值中删除双引号和\

时间:2015-07-30 16:04:49

标签: c# json jsonserializer

我编写了一个将列表转换为json的代码,但是我遇到了问题。当我测试web方法时,我发现我已将"\添加到嵌套对象中。我想知道如何使用JavaScriptSerializer()删除它们。

var innerResult (linq query to get list of data).toList()
var serializer = new JavaScriptSerializer();
var serializedResult = serializer.Serialize(innerResult);
result = (from value1 in db.Values
          join value2 in db.Values on value1.ParentId equals value2.RecordId
          join value3 in db.Values on value1.RecordId equals value3.ParentId
          where
              value1.ParentId
              == (from va in db.Values where va.Name == formType select va.RecordId).FirstOrDefault()
          orderby value1.RecordId
          select
              new ItemManagementViewModel
                  {
                      FormType = value2.Name,
                      FormControllerID = value1.RecordId,
                      FormControllerName = value1.Name,
                      values = serializedResult
                  }).ToList();
return result;

public class ItemManagementViewModel 
{
    public string FormType { get; set; }
    public long FormControllerID { get; set; }
    public string FormControllerName { get; set; }

    public string values { get; set; }
}

json fomat update如何在方括号之前删除双引号字符串" [{\

{
     "formType": "registarForm",
     "formControllerID": 2,
     "formControllerName": "Conditions",
     "values": "[{\"recordId\":14,\"name\":\"Asthma\",\"parentId\":14},{\"recordId\":15,\"name\":\"Cardiac Conditions\",\"parentId\":15},{\"recordId\":16,\"name\":\"COPD\",\"parentId\":16},{\"recordId\":17,\"name\":\"CVA\",\"parentId\":17}]"
}

1 个答案:

答案 0 :(得分:0)

如果您不想要额外的引号,则不应将内部结果序列化两次。即尝试使用下一个代码:

  select
      new ItemManagementViewModel
          {
              FormType = value2.Name,
              FormControllerID = value1.RecordId,
              FormControllerName = value1.Name,
              values = innerResult
          }).ToList();

并将'values'的类型更改为适当的类型,即List或YourEntities