使用Javascript查找并更新嵌套JSON数组和对象中的所有键值

Find and update all the keys value in a nested JSON Arrays and Objects using Javascript

本文关键字:对象 键值 数组 查找 Javascript 更新 JSON 嵌套 使用      更新时间:2024-02-04

我在下面有数组和对象的嵌套JSON数据

我需要在javascript或NodeJS或AngularJS 中将字符串中的所有键值从英语更新为西班牙语

    {
      "firstrootkey" : [ //Array of 6 objects 
      {  //1st object
        "key1" : "valueinstring",
        "key2" : randomnumbers
      },..... ],
      "secondrootkey" : { //having 7 objects 
        "One" : [ { //each object having array of n objects 
          "name" : "valueinstring",
          "id" : randomnumbers
        }, ......],
        "two two" : [ { // array of m objects
          "keya" : "valueinstring",
          "keyb" : randomnumbers
        },.......],
      .
      .
      .
      },
      "third root key" : { //having n objects 
        "sdfdsfsfs" : [ { //each object having array of n objects
          "keyc" : "valueinstring",
          "keyhuh" : 858556
          "hgjhgj" : 6789
        },.... ],
        .
        .
        .
        .
        .
        }
    }

我尝试了很多方法,但都不起作用
方法之一

   for (var rootKey in jsonEnglish) {
    var rootValue = jsonEnglish[rootKey];
      if (rootValue.isArray) { // function iterate Array objects         
        //loop each array
          // if every array is object -- function iterate object 
            // find value in object 
              //if value not array and object 
                 // then pass value to translator 
                  //and update the value with translated value
              //else if its object
                  //      

给出一些接近的指示

好的,首先在这里验证您的JSONhttp://jsonlint.com/

然后在javascript中,您可以访问json并将其与XMLHttpRequest()一起使用

var xmlhttp = new XMLHttpRequest();
        var url = "yourdata.json";
        xmlhttp.onreadystatechange = function() {
            if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
                var json_array = JSON.parse(xmlhttp.responseText);                    
                parseJson(json_array);
        }
    xmlhttp.open("GET", url, true);
    xmlhttp.send();
    json_data += "<p>Variable Access through array structure: " + json_array.firstrootkey[0].key1+ "</p>";
    json_data += "<p>Access Array inside array item: " + json_array.secondrootkey[0].one[0].name + "</p>";
    document.getElementById("display_json").innerHTML = json_data;

此外,第7行出现语法错误。此外,为了测试您的代码,您需要一个工作的web服务器。我更喜欢appserv。

以下是一些正确的JSON,并在JSONlint.com 上进行了验证

{
    "firstrootkey": [{
    "firstName": "Shane",
    "lastName": "Burton",
    "id": [
        1, 2, 3, 4, 5, 6, 7, 8
    ],
    "credits": "24"
}],
"secondrootkey": [{
    "One": [{
        "firstName": "Shane",
        "lastName": "Burton",
        "id": [
            1, 2, 3, 4, 5, 6, 7, 8
        ],
        "credits": "24"
    }]
}]
}

第二个JSON OBJECT每组括号代表数据库/列表中的一行

由于您使用的是foreach循环,为了简单起见,我会将其作为一个对象。

{
"foreachloopobject": [{
    "firstrootkey": [{
        "firstName": "Shane",
        "lastName": "Burton",
        "id": [
            1, 2, 3, 4, 5, 6, 7, 8
        ],
        "credits": "24"
    }],
    "secondrootkey": [{
        "One": [{
            "firstName": "Shane",
            "lastName": "Burton",
            "id": [
                1, 2, 3, 4, 5, 6, 7, 8
            ],
            "credits": "24"
        }]
    }],
    "firstrootkey": [{
        "firstName": "Shane",
        "lastName": "Burton",
        "id": [
            1, 2, 3, 4, 5, 6, 7, 8
        ],
        "credits": "24"
    }],
    "secondrootkey": [{
        "One": [{
            "firstName": "Shane",
            "lastName": "Burton",
            "id": [
                1, 2, 3, 4, 5, 6, 7, 8
            ],
            "credits": "24"
        }]
    }]
}]
}