如何从嵌套值中删除双引号和 \
how to remove double quote and \ from nested value
我已经编写了将列表转换为 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 格式更新如何删除方括号前的双引号字符串 "[{\
{
"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}]"
}
如果你不想要额外的引号,你不应该序列化内部结果两次。 IE。尝试使用下一个代码:
select
new ItemManagementViewModel
{
FormType = value2.Name,
FormControllerID = value1.RecordId,
FormControllerName = value1.Name,
values = innerResult
}).ToList();
并将 'values' 的类型更改为合适的类型,即 List 或 YourEntities
我已经编写了将列表转换为 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 格式更新如何删除方括号前的双引号字符串 "[{\
{
"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}]"
}
如果你不想要额外的引号,你不应该序列化内部结果两次。 IE。尝试使用下一个代码:
select
new ItemManagementViewModel
{
FormType = value2.Name,
FormControllerID = value1.RecordId,
FormControllerName = value1.Name,
values = innerResult
}).ToList();
并将 'values' 的类型更改为合适的类型,即 List 或 YourEntities