如何删除json的所有垃圾符号,以便我可以迭代它?

时间:2016-02-16 22:55:29

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

我已经从json对象生成了XML数组。但它包含' @'符号和"符号因为此jquery each()无法迭代。有没有办法清理这个。因为当我硬编码json数组并检查console.log()时,没有任何@symbols"但是如果json是从XML源生成的,那么它会添加那些垃圾符号。

我想知道的是,是否需要在不删除垃圾值的情况下迭代此xml?我的目的是提取值并使用一些新添加的属性重新生成一个新的json

这是当前生成的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: "}]}]}}).each(function(index, item))

你可以看到显示的json值有很多格式化垃圾值

这里是显示xml文档的视频链接以及生成json video link后浏览器显示的内容

这是XML文件

<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" />
    <helpNavNode Title=" Occasionally" ID="4" ParentID="2" Narrative="ALCOHOL CONSUMPTION: Occasionally" />
    <helpNavNode Title=" Socially" ID="5" ParentID="2" Narrative="ALCOHOL CONSUMPTION: Socially" />
    <helpNavNode Title=" Daily (3 beers/day) (6 pack/day)" ID="6" ParentID="2" Narrative="ALCOHOL CONSUMPTION: Daily (3 beers/day) (6 pack/day)" />
    <helpNavNode Title=" Is Alcoholic" ID="7" ParentID="2" Narrative="ALCOHOL CONSUMPTION: Is Alcoholic" />
    <helpNavNode Title=" History of Alcoholism" ID="8" ParentID="2" Narrative="ALCOHOL CONSUMPTION: History of Alcoholism" />
    <helpNavNode Title=" None" ID="26" ParentID="2" Narrative="ALCOHOL CONSUMPTION: None" />
    <helpNavNode 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? " />
    </helpNavNode>
  </helpNavNode>
  <helpNavNode 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" />
    </helpNavNode>
    <helpNavNode Title=" Divorced" ID="11" ParentID="9" Narrative="MARITAL STATUS: Divorced" />
    <helpNavNode Title=" Single" ID="12" ParentID="9" Narrative="MARITAL STATUS: Single" />
  </helpNavNode>
  <helpNavNode Title="Recreational drug use" ID="13" ParentID="1" Narrative=":Recreational drug use" />
  <helpNavNode Title="OCCUPATION" ID="14" ParentID="1">
    <helpNavNode Title=" works for  x 27 years" ID="25" ParentID="14" Narrative="OCCUPATION: works for  x 27 years" />
  </helpNavNode>
  <helpNavNode Title=" 1 child" ID="16" ParentID="1" Narrative=": 1 child" />
  <helpNavNode Title=" 2/3/4/5/6 children" ID="17" ParentID="1" Narrative=": 2/3/4/5/6 children" />
  <helpNavNode Title="Activities" ID="18" ParentID="1">
    <helpNavNode Title=" walks regularly" ID="19" ParentID="18" Narrative="Activities: walks regularly" />
    <helpNavNode Title=" cycles for exercise" ID="20" ParentID="18" Narrative="Activities: cycles for exercise" />
    <helpNavNode Title=" runs 3 days/week" ID="21" ParentID="18" Narrative="Activities: runs 3 days/week" />
    <helpNavNode Title=" hunting and fishing" ID="22" ParentID="18" Narrative="Activities: hunting and fishing" />
    <helpNavNode Title=" hiking and camping" ID="23" ParentID="18" Narrative="Activities: hiking and camping" />
    <helpNavNode Title=" swims regularly" ID="24" ParentID="18" Narrative="Activities: swims regularly" />
  </helpNavNode>
  <helpNavNode 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" />
    </helpNavNode>
    <helpNavNode Title="Quit Date" ID="35" ParentID="27" Narrative="Tobacco Use:Quit Date" />
    <helpNavNode Title="Year Started" ID="36" ParentID="27" Narrative="Tobacco Use:Year Started" />
  </helpNavNode>
  <helpNavNode 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: " />
    <helpNavNode Title=" Living situation:  Occupation:  Tobacco:  EtOH:  Rec. drugs: " ID="42" ParentID="37" Narrative="SocHx  Template: Living situation:  Occupation:  Tobacco:  EtOH:  Rec. drugs: " />
  </helpNavNode>
</class>

这里是在视图中生成json的代码

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

这里是访问生成的json对象的jquery代码:

@section Scripts {
    <script src="Scripts/jstree.min.js"></script>
    <script>
        $(document).ready(function () {

            console.log( @jsonObj);

            $(@jsonObj).each(function (index, item) {
                console.log( item[0] );
            });
          });
</script>
}

0 个答案:

没有答案