jon dynke
jon dynke

Reputation: 33

why i cannot pass an @ symbol in the replace()?

I have the following javascript code written in the @section of my view.

var mapping = {
    '@Title': 'text',
    '@ID': 'id',
    '@ParentID': 'parent',
    '@Narrative': 'value'
}
var temp = JSON.stringify(data);
for (key in mapping) {
    temp = temp.replace(new RegExp(key, 'g'), mapping[key]);

}
data = JSON.parse(temp.replace(/@/g, ''));

Assume that data is an array already declared above. In the View, I cannot use the statement data = JSON.parse(temp.replace(/@/g, ''));. It throws the following error:

Parser Error

Description: An error occurred during the parsing of a resource required to service this request. Please review the following specific parse error details and modify your source file appropriately.

Parser Error Message: "/" is not valid at the start of a code block. Only identifiers, keywords, comments, "(" and "{" are valid.

The reason I pass an @ is to remove all the @ symbols from the temp, but with the MVC code it throws an error with the @ symbols.

Here is some array data this was originally posted by zeelong in his post.

var data = {
    "class": {
        "@Title": "SOCIAL HISTORY",
        "@ID": "1",
        "@ParentID": "0",
        "helpNavNode": [{
            "@Title": "ALCOHOL CONSUMPTION",
            "@ID": "2",
            "@ParentID": "1",
            "helpNavNode": [{
                "@Title": " Never",
                "@ID": "3",
                "@ParentID": "2",
                "@Narrative": "ALCOHOL CONSUMPTION: Never"
            }, {
                "@Title": " Occasionally",
                "@ID": "4",
                "@ParentID": "2",
                "@Narrative": "ALCOHOL CONSUMPTION: Occasionally"
            }, {
                "@Title": " Socially",
                "@ID": "5",
                "@ParentID": "2",
                "@Narrative": "ALCOHOL CONSUMPTION: Socially"
            }, {
                "@Title": " Daily (3 beers/day) (6 pack/day)",
                "@ID": "6",
                "@ParentID": "2",
                "@Narrative": "ALCOHOL CONSUMPTION: Daily (3 beers/day) (6 pack/day)"
            }, {
                "@Title": " Is Alcoholic",
                "@ID": "7",
                "@ParentID": "2",
                "@Narrative": "ALCOHOL CONSUMPTION: Is Alcoholic"
            }, {
                "@Title": " History of Alcoholism",
                "@ID": "8",
                "@ParentID": "2",
                "@Narrative": "ALCOHOL CONSUMPTION: History of Alcoholism"
            }, {
                "@Title": " None",
                "@ID": "26",
                "@ParentID": "2",
                "@Narrative": "ALCOHOL CONSUMPTION: None"
            }, {
                "@Title": "Alcoholism Screen Question",
                "@ID": "39",
                "@ParentID": "2",
                "helpNavNode": {
                    "@Title": " Ever had a drinking problem? Last drink? ",
                    "@ID": "40",
                    "@ParentID": "39",
                    "@Narrative": "ALCOHOL CONSUMPTION:Alcoholism Screen Question: Ever had a drinking problem? Last drink? "
                }
            }]
        }, {
            "@Title": "MARITAL STATUS",
            "@ID": "9",
            "@ParentID": "1",
            "helpNavNode": [{
                "@Title": " Married",
                "@ID": "10",
                "@ParentID": "9",
                "helpNavNode": {
                    "@Title": " x 10 years",
                    "@ID": "15",
                    "@ParentID": "10",
                    "@Narrative": "MARITAL STATUS: Married: x 10 years"
                }
            }, {
                "@Title": " Divorced",
                "@ID": "11",
                "@ParentID": "9",
                "@Narrative": "MARITAL STATUS: Divorced"
            }, {
                "@Title": " Single",
                "@ID": "12",
                "@ParentID": "9",
                "@Narrative": "MARITAL STATUS: Single"
            }]
        }, {
            "@Title": "Recreational drug use",
            "@ID": "13",
            "@ParentID": "1",
            "@Narrative": ":Recreational drug use"
        }, {
            "@Title": "OCCUPATION",
            "@ID": "14",
            "@ParentID": "1",
            "helpNavNode": {
                "@Title": " works for  x 27 years",
                "@ID": "25",
                "@ParentID": "14",
                "@Narrative": "OCCUPATION: works for  x 27 years"
            }
        }, {
            "@Title": " 1 child",
            "@ID": "16",
            "@ParentID": "1",
            "@Narrative": ": 1 child"
        }, {
            "@Title": " 2/3/4/5/6 children",
            "@ID": "17",
            "@ParentID": "1",
            "@Narrative": ": 2/3/4/5/6 children"
        }, {
            "@Title": "Activities",
            "@ID": "18",
            "@ParentID": "1",
            "helpNavNode": [{
                "@Title": " walks regularly",
                "@ID": "19",
                "@ParentID": "18",
                "@Narrative": "Activities: walks regularly"
            }, {
                "@Title": " cycles for exercise",
                "@ID": "20",
                "@ParentID": "18",
                "@Narrative": "Activities: cycles for exercise"
            }, {
                "@Title": " runs 3 days/week",
                "@ID": "21",
                "@ParentID": "18",
                "@Narrative": "Activities: runs 3 days/week"
            }, {
                "@Title": " hunting and fishing",
                "@ID": "22",
                "@ParentID": "18",
                "@Narrative": "Activities: hunting and fishing"
            }, {
                "@Title": " hiking and camping",
                "@ID": "23",
                "@ParentID": "18",
                "@Narrative": "Activities: hiking and camping"
            }, {
                "@Title": " swims regularly",
                "@ID": "24",
                "@ParentID": "18",
                "@Narrative": "Activities: swims regularly"
            }]
        }, {
            "@Title": "Tobacco Use",
            "@ID": "27",
            "@ParentID": "1",
            "helpNavNode": [{
                "@Title": "Cigar, Cigarette, Pipe, Smokeless",
                "@ID": "30",
                "@ParentID": "27",
                "helpNavNode": {
                    "@Title": "10/pk year",
                    "@ID": "32",
                    "@ParentID": "30",
                    "@Narrative": "Tobacco Use:Cigar, Cigarette, Pipe, Smokeless:10/pk year"
                }
            }, {
                "@Title": "Quit Date",
                "@ID": "35",
                "@ParentID": "27",
                "@Narrative": "Tobacco Use:Quit Date"
            }, {
                "@Title": "Year Started",
                "@ID": "36",
                "@ParentID": "27",
                "@Narrative": "Tobacco Use:Year Started"
            }]
        }, {
            "@Title": "SocHx  Template",
            "@ID": "37",
            "@ParentID": "1",
            "helpNavNode": [{
                "@Title": " Living situation:  Occupation:  Tobacco:  EtOH:  Rec. drugs: ",
                "@ID": "41",
                "@ParentID": "37",
                "@Narrative": "SocHx  Template: Living situation:  Occupation:  Tobacco:  EtOH:  Rec. drugs: "
            }, {
                "@Title": " Living situation:  Occupation:  Tobacco:  EtOH:  Rec. drugs: ",
                "@ID": "42",
                "@ParentID": "37",
                "@Narrative": "SocHx  Template: Living situation:  Occupation:  Tobacco:  EtOH:  Rec. drugs: "
            }]
        }]
    }
}

Upvotes: 0

Views: 89

Answers (2)

Sam FarajpourGhamari
Sam FarajpourGhamari

Reputation: 14741

This is happening because Razor thinks you are trying to write C# code. Simply add double @ to tell Razor dose not think so.

data = JSON.parse(temp.replace(/@@/g, ''));

Also do same for mapping variable content:

var mapping = {
    '@@Title': 'text',
    '@@ID': 'id',
    '@@ParentID': 'parent',
    '@@Narrative': 'value'
}

Upvotes: 2

8eecf0d2
8eecf0d2

Reputation: 1579

Assuming you want to remove all @'s, there's no need to loop over each property and remove the @'s individually, once you stringify the data you can remove them in one swoop.

var obj = {
  '@a': 'val1',
  '@b': 'val2'
}

var stringy = JSON.stringify(obj)

var replaced = stringy.replace(/@/g, '')

var parsed = JSON.parse(replaced)

console.log(obj, stringy, replaced, parsed)

var oneline = JSON.parse(JSON.stringify(obj).replace(/@/g, ''))

console.log(oneline)

Upvotes: 0

Related Questions