如何正确显示具有相同名称的JSON对象

时间:2017-10-10 21:21:36

标签: javascript html json

我正在尝试使用Javascript显示一个JSON对象,它一切正常,但它不会在var vb6book中打印第二个作者,我敢肯定我只是把它写错了但是我没有了解如何修复它...我需要将该对象中的两个作者打印到我正在做的作业页面...请帮忙!谢谢!

这是JS代码:

#include <iostream>
using namespace std;

main() {
   int arr[] = {1,3,5,7,8,9,6};
   int item = 10, k = 3, n = 7;
   int i = 0, j = n;

   cout<<"The original array elements are :\n";

   for(i = 0; i<n; i++) {
      cout<<"arr["<<i<<"] = "<< arr[i] <<"\n";
   }

   n = n + 1;

   while( j >= k) {
      arr[j+1] = arr[j];
      j = j - 1;
   }

   arr[k] = item;

   cout<<"The array elements after insertion :\n";

   for(i = 0; i<n; i++) {
      cout<<"arr["<<i<<"] = "<< arr[i] <<"\n";
   }
}

这是HTML:

  var javabook = {
  "book": {
    "isbn" : "0-596-00016-2",
    "title"  : "Java and XML",
    "price"       : "39.95",
    "publisher"  : "O'Reilly &amp; Associates",
    "copyright": "2000",
      "authors": {
        "author": {
          "fname": "Brett",
          "lname": "McLaughlin",
          "price": "22.00"
        }
      }
    }
};

var vb6book = {
  "book": {
    "isbn" : "1-861003-32-3",
    "title"  : "Professional Visual Basic 6 XML",
    "price"       : "49.99",
    "publisher"  : "Wrox Press",
    "copyright": "2000",
      "authors": {
        "author": {
          "fname": "James",
          "lname": "Britt",
          "price": ""
        }
        "author": {
          "fname": "Tuen",
          "lname": "Duynstee",
          "price": ""
        }
      }
    }
};

var myJSON = JSON.stringify(javabook);
var myJSON2 = JSON.stringify(vb6book);

document.getElementById("demo").innerHTML = myJSON;
document.getElementById("demo2").innerHTML = myJSON2;

3 个答案:

答案 0 :(得分:0)

您不能拥有冗余命名的属性键。我建议使用一个数组;像这样的东西:

"authors": [{
          "fname": "James",
          "lname": "Britt",
          "price": ""
        }, {
          "fname": "Tuen",
          "lname": "Duynstee",
          "price": ""
        }]

答案 1 :(得分:0)

第一个JSON只有一个作者,这就是为什么它没有错误,第二个有错误,因为作者的数组应该像这样写。 "Author": [ { "name":"author0", "otherArrayIntoThis":[ "subArrayAuthor0", "subArrayAuthor1", "subArrayAuthor2" ] }, { "name":"author1", "otherArrayIntoThis":[ "subArrayAuthor0", "subArrayAuthor1", "subArrayAuthor2"] }, { "name":"author2", "otherArrayIntoThis":[ "subArrayAuthor0", "subArrayAuthor1", "subArrayAuthor2"] } ]

此外,您可以使用相同的表示法,没有2个JSON,只有一个变量存储整本书,并以纯JSON格式通过点符号访问信息。

 `"books": [{
"isbn" : "0-596-00016-2",
"title"  : "Java and XML",
"price"       : "39.95",
"publisher"  : "O'Reilly &amp; Associates",
"copyright": "2000",
  "authors": {
    "author": {
      "fname": "Brett",
      "lname": "McLaughlin",
      "price": "22.00"
    }
  }
},{
"isbn" : "1-861003-32-3",
"title"  : "Professional Visual Basic 6 XML",
"price"       : "49.99",
"publisher"  : "Wrox Press",
"copyright": "2000",
  "authors": {
    "author": {
      "fname": "James",
      "lname": "Britt",
      "price": ""
    },
    "author": {
      "fname": "Tuen",
      "lname": "Duynstee",
      "price": ""
    }
  }
}]`

答案 2 :(得分:0)

我尝试过运行你的代码。

&#34;作者&#34;之后有一个逗号丢失。在vb6book

var vb6book = {
  "book": {
    "isbn" : "1-861003-32-3",
    "title"  : "Professional Visual Basic 6 XML",
    "price"       : "49.99",
    "publisher"  : "Wrox Press",
    "copyright": "2000",
      "authors": {
        "author": {
          "fname": "James",
          "lname": "Britt",
          "price": ""
        }, // <--- I added a comma here
        "author": { // <--- This author key is a duplicate.
          "fname": "Tuen",
          "lname": "Duynstee",
          "price": ""
        }
      }
    }
};

如果您尝试此代码:

console.log(vb6book['book']['authors'])

输出结果为:

{"author":{"fname":"Tuen","lname":"Duynstee","price":""}}

您有一个重复的"author"密钥。