如何在json中重命名密钥名称并重新创建它?

时间:2016-02-17 04:32:15

标签: jquery asp.net-mvc asp.net-mvc-4

我有以下json数组(原始数据来自ASP MVC作为XML,但我已将它们转换为json)

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

我想对此做两件事:

  1. 我想从显示的位置删除@符号
  2. 我需要将密钥名称更改为以下内容:
  3. @Title =>文本

    @ID => ID

    @ParentID =>父

    @Narrative =>值

    有没有我可以做到这一点?目前我不知道他们是怎么做到的?

    更新:

    json是如何创建的。 XMLDocument来自BLL类,并且以下内容放在客户端上,硬编码的XML文件具有从BLL返回的内容的副本,因此很容易在客户端中检查它(从我的角度来看)< / p>

    @{
     var xdoc = new XmlDocument();
        xdoc.Load(Server.MapPath(Url.Content(@"~/App_Data/XMLFile1.xml")));
        var jsonObj=JsonConvert.SerializeXmlNode(xdoc);
    }
    

1 个答案:

答案 0 :(得分:1)

您可以通过对JSON进行字符串化并执行字符串操作来实现,如下所示:

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, ''));

&#13;
&#13;
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: "
      }]
    }]
  }
}
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, ''));
console.log(data);
&#13;
&#13;
&#13;