【问题标题】:How to parse Json String in angularJS? [closed]如何在 angularJS 中解析 Json 字符串? [关闭]
【发布时间】:2014-01-06 10:53:32
【问题描述】:

我有一个类似的 nodeJSON 字符串

{
  "Name": "Addition",
  "Id": "3",
  "ParentId": "1",
  "children": [
    {
      "Name": "Two Numbers",
      "Id": "5",
      "ParentId": "3",
      "children": []
    },
    {
      "Name": "Three Numbers",
      "Id": "6",
      "ParentId": "3",
      "children": []
    }
  ]

}

我将 parentId 设为 3,然后我想将键值名称显示为“Addition”,该名称应与其 Id 匹配。

{
      "Name": "Addition",
      "Id": "3",
      "ParentId": "1",
      "children": [....]
}

myApp.controller('leafController', function($scope){

$scope.getname = function(parentid,nodeJSON )
 { 
   //parentid is 3 here
    $scope.Id = parentid; 
    $scope.data = nodeJSON ;
    // i want to get name according to id
    // Name : Addition (if Id is 3) 

 };


  });

我被打动了。

请帮帮我,
提前致谢

【问题讨论】:

标签: javascript json node.js angularjs treeview


【解决方案1】:

循环children 数组并返回带有Id = 3 的子元素,函数如下:

function getChildrenById(children, value) {
     for (var i=0, iLen=children.length; i<iLen; i++) {

        if (arr[i].Id == value) return arr[i];
      }
    }
}

并称它为getChildrenById(children, 3)

【讨论】:

  • 它不起作用我想将整个 json 字符串传递给函数,并且需要通过 id 查找名称。还有其他方法可以继续吗?
猜你喜欢
  • 2014-01-04
  • 2021-05-24
  • 1970-01-01
  • 2015-06-25
  • 1970-01-01
  • 2012-10-23
  • 2014-06-29
  • 2022-01-20
  • 1970-01-01
相关资源
最近更新 更多