1

We have an application with knockout and we are facing a problem that some registers in the database have double quotes, which caused JSON parsing to fail.

Here's my json that is not valid due to a rogue double quote:

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

If you test this json against jsonlint you will see where the problem lies. What is the best way to handle this? I think the way I'm serializing the C# model to JSON is not proper? To do that I use:

var jsonModel = JsonConvert.SerializeObject(Model);

Any help is very appreciated.

EDIT

Issue was fixed. Problem was with the serialization.

I fixed this by using the method HttpUtility.JavaScriptStringEncode

var jsonViewModel = HttpUtility.JavaScriptStringEncode(Json.Encode(Model));

This solved my problem. All I had to do to pass this to knockout was @Html.Raw(jsonViewModel)

Best regards and thanks everyone!

Daniel

DAG
  • 2,460
  • 5
  • 33
  • 61
  • Maybe some help here: http://stackoverflow.com/questions/12042302/serializing-strings-containing-apostrophes-with-json-net – Roy J Aug 16 '15 at 18:29
  • Or maybe: http://stackoverflow.com/questions/22293208/turn-c-sharp-object-to-json-string-how-to-handle-double-quotation#answer-22296774 – Roy J Aug 16 '15 at 18:31
  • so where is the extra double quote coming from? json.net? knockout? the database? can you intercept it and escape it with a slash? – viggity Aug 16 '15 at 19:40
  • double quote comes from the database. I can intercept it in c# view model and escape it, but I don't know if that is the best way to fix this issue. what if I have tomorrow a new property which also accepts double quotes? that I have to escape it also manually. I would rather change the save method to scape all special chars, but the save method is not ours. – DAG Aug 16 '15 at 19:55
  • I think you'll need to show sample data and code that causes this issue, or we won't be able to help you prevent it. (As a side note, please trim your samples to a *minimal* version - now you require of us that we go through that entire slab of code, while only a few lines are important...) – Jeroen Aug 17 '15 at 06:20
  • Use jsonlint and you can see where is the issue! – DAG Aug 17 '15 at 06:22

0 Answers0