首页 文章

如何在javascript中从服务器迭代json响应值

提问于
浏览
0

我是新手javascript我有一个Web请求,它给出了JSON格式的响应,任务是我需要将数据解析成数组

here is my sample reponse :

"Employee" : {
  "Employee_Names" : [
     {
        "BAR_RATING" : "0",
        "Name" : "anand",
        "Name" : "0",
        "PATTERN" : "Ln",

     },
     {
        "BAR_RATING" : "0",
        "Name" : "av",
        "Name_RATING" : "0",
        "PATTERN" : "FiLi",

     },
     {
        "BAR_RATING" : "0",
        "Name" : "books",
        "Name_RATING" : "0",
        "PATTERN" : "Ln",

     },
     {
        "BAR_RATING" : "0",
        "Name" : "kanagalu",
        "Name_RATING" : "0",
        "PATTERN" : null,

     },
     {
        "BAR_RATING" : "0",
        "Name" : "specialty-av",
        "Name_RATING" : "0",
        "PATTERN" : "Fn-Ln",

     }
  ],
  "FOUND_Name" : [ null ],
  "OTHER_Name" : [
     {
        "BAR_RATING" : "0",
        "Name" : "kindle-cs-support",
        "Name_RATING" : "0",
        "PATTERN" : null,

     },
     {
        "BAR_RATING" : "0",
        "Name" : "noreply-ops-jobs",
        "Name_RATING" : "0",
        "PATTERN" : null,

     }
  ],
  "PERSONAL_Name" : [ null ],
  "PROJECTED_Name" : [
     {
        "BAR_RATING" : "0",
        "Name" : "anand.venkatesan",
        "Name_RATING" : "0",
        "PATTERN" : "Fn.Ln",

     },
     {
        "BAR_RATING" : "0",
        "Name" : "anandv",
        "Name_RATING" : "0",
        "PATTERN" : "FnLi",

     },
     {
        "BAR_RATING" : "0",
        "Name" : "vanand",
        "Name_RATING" : "0",
        "PATTERN" : "LiFn",

     }
  ]
  },

我需要的是我需要从 Employee_Names 对象我希望所有名称在一个数组中类似从 OTHER_Name 我希望所有名称存储在数组中

我不知道如何用动态数组大小解析数据

3 回答

  • 2

    使用Array map()方法将对象数组中的所有特定属性值转换为单个数组 .

    To fetch all the names into an single array from Employee_Names :

    var empNames = employee.Employee_Names.map(function(item) {
        return item.Name;
      });
    

    To fetch all the names into an single array from OTHER_Name :

    var otherNames = employee.OTHER_Name.map(function(item) {
        return item.Name;
      });
    

    Working Demo

    var employee = {
      "Employee_Names" : [
         {
            "BAR_RATING" : "0",
            "Name" : "anand",
            "Name_RATING" : "0",
            "PATTERN" : "Ln",
    
         },
         {
            "BAR_RATING" : "0",
            "Name" : "av",
            "Name_RATING" : "0",
            "PATTERN" : "FiLi",
    
         },
         {
            "BAR_RATING" : "0",
            "Name" : "books",
            "Name_RATING" : "0",
            "PATTERN" : "Ln",
    
         },
         {
            "BAR_RATING" : "0",
            "Name" : "kanagalu",
            "Name_RATING" : "0",
            "PATTERN" : null,
    
         },
         {
            "BAR_RATING" : "0",
            "Name" : "specialty-av",
            "Name_RATING" : "0",
            "PATTERN" : "Fn-Ln",
    
         }
      ],
      "FOUND_Name" : [ null ],
      "OTHER_Name" : [
         {
            "BAR_RATING" : "0",
            "Name" : "kindle-cs-support",
            "Name_RATING" : "0",
            "PATTERN" : null,
    
         },
         {
            "BAR_RATING" : "0",
            "Name" : "noreply-ops-jobs",
            "Name_RATING" : "0",
            "PATTERN" : null,
    
         }
      ]
      };
      
      var empNames = employee.Employee_Names.map(function(item) {
        return item.Name;
      });
      
      var otherNames = employee.OTHER_Name.map(function(item) {
        return item.Name;
      });  
      
      console.log("Employee Names", empNames);
      console.log("Other Names", otherNames);
    
  • 1

    从Employee.Employee_Names数组中获取字段“name”中的所有值

    var resultArray = Employee.Employee_Names.map(function(a) {return a.Name;});
    

    resultArray将是 ["anand", "av", "books", "kanagalu", "specialty-av" ]

  • 2

    如果我理解正确,这应该适合您的需要 .

    我们遍历 "Employee_Names" 个对象 .

    // ES6 code    
    var allNames = employee["Employee_Names"].map(bar => bar.Name);
    
    // pure javascript
    var allNames = [];
    for (i = 0; i < employee["Employee_Names"].length; i++) {
      var element = employee["Employee_Names"][i];
    
      // We push only name
      allNames.push( element.Name );
    
      // We push full json object
       //allNames.push( element );
    }
    

    现在我们使用 allNames 数组分配新的json密钥 .

    employee = Object.assign(employee, { "NEW_NAMES" : allNames });
    

    检查工作例子:

    var employee = {
      "Employee_Names" : [
         {
            "BAR_RATING" : "0",
            "Name" : "anand",
            "Name_RATING" : "0",
            "PATTERN" : "Ln",
    
         },
         {
            "BAR_RATING" : "0",
            "Name" : "av",
            "Name_RATING" : "0",
            "PATTERN" : "FiLi",
    
         },
         {
            "BAR_RATING" : "0",
            "Name" : "books",
            "Name_RATING" : "0",
            "PATTERN" : "Ln",
    
         },
         {
            "BAR_RATING" : "0",
            "Name" : "kanagalu",
            "Name_RATING" : "0",
            "PATTERN" : null,
    
         },
         {
            "BAR_RATING" : "0",
            "Name" : "specialty-av",
            "Name_RATING" : "0",
            "PATTERN" : "Fn-Ln",
    
         }
      ],
      "FOUND_Name" : [ ],
      "OTHER_Name" : [
         {
            "BAR_RATING" : "0",
            "Name" : "kindle-cs-support",
            "Name_RATING" : "0",
            "PATTERN" : null,
    
         },
         {
            "BAR_RATING" : "0",
            "Name" : "noreply-ops-jobs",
            "Name_RATING" : "0",
            "PATTERN" : null,
    
         }
      ],
      "PERSONAL_Name" : [  ],
      "PROJECTED_Name" : [
         {
            "BAR_RATING" : "0",
            "Name" : "anand.venkatesan",
            "Name_RATING" : "0",
            "PATTERN" : "Fn.Ln",
    
         },
         {
            "BAR_RATING" : "0",
            "Name" : "anandv",
            "Name_RATING" : "0",
            "PATTERN" : "FnLi",
    
         },
         {
            "BAR_RATING" : "0",
            "Name" : "vanand",
            "Name_RATING" : "0",
            "PATTERN" : "LiFn",
    
         }
      ]
      };
    
    
    // ES6
    //var allNames = employee["Employee_Names"].map(bar => bar.Name);
    
    // pure javascript
    var allNames = [];
    for (i = 0; i < employee["Employee_Names"].length; i++) {
      var element = employee["Employee_Names"][i];
    
      // We push only name
      allNames.push( element.Name );
      
      // We push full json object
       //allNames.push( element );
    }
    
    // Now we assign new json key with our names
    employee = Object.assign(employee, { "NEW_NAMES" : allNames });
    
    // Our new employee json
    console.log(employee);
    

相关问题