处理JSON字符串中的双引号

时间:2015-08-16 17:55:09

标签: c# json asp.net-mvc c#-4.0

我们有一个带淘汰的应用程序,我们遇到的问题是数据库中的某些寄存器有双引号,导致JSON解析失败。

这里的我的json由于流氓双引号而无效:

{
"OptionSummaries": [
    {
        "Id": 110,
        "Name": "Option 1",
        "Status": 1,
        "ProductGroupNodes": [
            {
                "Id": 110,
                "Name": "Corporate Brand Reputation",
                "Status": 2,
                "Waves": [
                    {
                        "Id": 110,
                        "Name": "Wave 1",
                        "Status": 2,
                        "Services": [
                            {
                                "Id": 1101,
                                "Title": "Proposal Budget Owner Service",
                                "CurrencyCode": "USD",
                                "EstimatedCost": 177.0000,
                                "CreationDateTime": "/Date(1437472898503)/",
                                "Status": 2
                            }
                        ]
                    }
                ]
            },
            {
                "Id": 111,
                "Name": "2013 Consumer Scan",
                "Status": 1,
                "Waves": [
                    {
                        "Id": 111,
                        "Name": "Wave 1",
                        "Status": 1,
                        "Services": [
                            {
                                "Id": 1111,
                                "Title": "Proposal Budget Owner Service",
                                "CurrencyCode": "USD",
                                "EstimatedCost": 0.0000,
                                "CreationDateTime": "/Date(1437472898503)/",
                                "Status": 1
                            }
                        ]
                    }
                ]
            }
        ]
    },
    {
        "Id": 115,
        "Name": "Option 2",
        "Status": 1,
        "ProductGroupNodes": [
            {
                "Id": 115,
                "Name": "Corporate Brand Reputation",
                "Status": 1,
                "Waves": [
                    {
                        "Id": 115,
                        "Name": "Wave 1",
                        "Status": 1,
                        "Services": [
                            {
                                "Id": 1151,
                                "Title": "Proposal Budget Owner Service",
                                "CurrencyCode": "USD",
                                "EstimatedCost": 0.0000,
                                "CreationDateTime": "/Date(1437472898503)/",
                                "Status": 1
                            }
                        ]
                    }
                ]
            },
            {
                "Id": 116,
                "Name": "2013 Consumer Scan",
                "Status": 1,
                "Waves": [
                    {
                        "Id": 116,
                        "Name": "Wave 1",
                        "Status": 1,
                        "Services": [
                            {
                                "Id": 1161,
                                "Title": "Proposal Budget Owner Service",
                                "CurrencyCode": "USD",
                                "EstimatedCost": 0.0000,
                                "CreationDateTime": "/Date(1437472898503)/",
                                "Status": 1
                            }
                        ]
                    }
                ]
            }
        ]
    }
],
"ServiceCostsEdit": {
    "ServiceId": 1101,
    "ServiceName": "Proposal Budget Owner Service",
    "ServiceCurrencyIsoCode": "USD",
    "ServiceLegalEntityCode": "0310",
    "LaborHourCostsPanel": {
        "LaborHourCosts": [
            {
                "Id": 2,
                "CostCenters": [
                    {
                        "Disabled": false,
                        "Group": null,
                        "Selected": false,
                        "Text": "3101010001 - Consumer KAM Group",
                        "Value": "3101010001"
                    },
                    {
                        "Disabled": false,
                        "Group": null,
                        "Selected": false,
                        "Text": "3102510255 - Knowledge Panel",
                        "Value": "3102510255"
                    }
                ],
                "FiscalYears": [
                    {
                        "Disabled": false,
                        "Group": null,
                        "Selected": false,
                        "Text": "2013",
                        "Value": "2013"
                    }
                ],
                "LaborGrades": [
                    {
                        "Code": "0HL002",
                        "CurrencyCode": "USD",
                        "Rate": 44.0000,
                        "Disabled": false,
                        "Group": null,
                        "Selected": false,
                        "Text": "0HL002 - 44.00/hr USD",
                        "Value": "0HL002"
                    }
                ],
                "SelectedLaborGradeCostCenter": "3101010001",
                "SelectedLaborGradeFiscalYear": 2013,
                "SelectedLaborGradeCode": "0HL002",
                "SelectedLaborGradeRate": 44.0000,
                "SetupHours": 2.00,
                "ManagementHours": 1.00,
                "DeliveryHours": 1.00,
                "IsEmpty": false
            }
        ],
        "LaborHourCostsCostCenterDataSource": [
            {
                "Disabled": false,
                "Group": null,
                "Selected": false,
                "Text": "3101010001 - Consumer KAM Group",
                "Value": "3101010001"
            },
            {
                "Disabled": false,
                "Group": null,
                "Selected": false,
                "Text": "3102510255 - Knowledge Panel",
                "Value": "3102510255"
            }
        ],
        "DefaultCostCenterCodeForInitialEmptyRecord": null,
        "DefaultFiscalYearForInitialEmptyRecord": null,
        "OverheadCosts": [
            {
                "LaborGradeCostCenterCode": "3101010001",
                "LaborGradeFiscalYear": 2013,
                "OverheadRate": 0.0000,
                "SetupHours": 2.00,
                "ManagementHours": 1.00,
                "DeliveryHours": 1.00
            }
        ],
        "OverheadCostsVisible": true,
        "OverheadCostsInitialDataSource": [
            {
                "Key": {
                    "CostCenterCode": "3101010001",
                    "FiscalYear": 2013
                },
                "Value": {
                    "Code": "0HO001",
                    "CurrencyCode": "USD",
                    "Rate": 0.00,
                    "Disabled": false,
                    "Group": null,
                    "Selected": false,
                    "Text": "0HO001 - USD 0.00/hr",
                    "Value": "0HO001"
                }
            }
        ],
        "CostCenter": "F2F PAPI",
        "ServiceCurrencyIsoCode": "USD",
        "EditingAllowed": true
    },
    "VendorCostsPanel": {
        "VendorCosts": [
            {
                "Id": 132,
                "SelectedCostElementCode": "1992000004",
                "VendorCode": "",
                "VendorName": null,
                "Description": "doublequotes"","DirectCostAttachment":null,"Quantity":1,"VendorRate":1.0000,"IsEmpty":false}],"CostElements":[{"Disabled":false,"Group":null,"Selected":false,"Text":"ExternalSuppliercosts","Value":"1992000004"},{"Disabled":false,"Group":null,"Selected":false,"Text":"Licensesfromaffilitatedcompanies","Value":"1992000002"}],"ServiceCurrencyIsoCode":"USD","EditingAllowed":true},"CostingAssumptionsPanel":{"Description":"","EditingAllowed":true},"ServiceSpecificationsPanel":{"Title":"ProposalBudgetOwnerService","Type":"ProposalBudgetOwnerService","Fields":[{"Value":"Hellolonglonglongtext...","Id":153,"Code":"OVERVIEW","Title":"Overview","IsRequiredForCosting":false,"DependencyVisibilityExpression":null,"FieldCodesToReevaluateOnChange":[],"EditingAllowed":false}]},"ApprovalComments":{"CommentType":0,"Message":null,"ApproverName":null,"ApproverEmail":null,"AnyComments":false},"RejectedCostInfoVisible":false,"EditingAllowed":true},"SubmitCostsEnabled":true,"EditingAllowed":false,"SelectedTreeNode":{"Id":1101,"Type":3},"Proposal":{"ProposalId":11,"ProposalName":"Ad-hocatCostingonMain","ProposalCostCenterCode":"3102510220","ProposalValid":{"IsValid":true,"ErrorMessage":""},"SoldToCustomer":"JenniferSamson","ExpectedProjectStartDate":"/Date(1426892400000)/","ExpectedProjectEndDate":"/Date(1431208800000)/","FunctionalityAreaEnabled":true}}

如果您针对 jsonlint 测试此json,您将看到问题所在。处理这个问题的最佳方法是什么?我认为我将C#模型序列化为JSON的方式不合适?要做到这一点,我使用:

var jsonModel = JsonConvert.SerializeObject(Model);

非常感谢任何帮助。

修改

问题已修复。问题在于序列化。

我使用 HttpUtility.JavaScriptStringEncode

方法解决了这个问题。
var jsonViewModel = HttpUtility.JavaScriptStringEncode(Json.Encode(Model));

这解决了我的问题。我必须做的就是把它传递到淘汰赛 @ Html.Raw(jsonViewModel)

致以最诚挚的问候并感谢大家!

丹尼尔

0 个答案:

没有答案