简体   繁体   English

As you can see my json object i want to put my json object in a text file and let my js code read my json object from a text file

[英]As you can see my json object i want to put my json object in a text file and let my js code read my json object from a text file

As you can see my json object i want to put my json object in a text file and let my js code read my json object from a text file but i dont know how to do it pls help As you can see my json object i want to put my json object in a text file and let my js code read my json object from a text file but i dont know how to do it pls help

var objs = [{
  "Object": {
    "ID": 1,
    "type": "button",
    "color": "red",
    "Text": "I Love",
    "width": "100px",
    "height": "100px",
    "Font": {
      "fontName": "tahoma",
      "font": "150px"
    }
  },
  "Object ": {
    "ID": 2,
    "type": "button",
    "color": "red",
    "Text": "I Love",
    "width": "100px",
    "height": "100px",
    "Font": {
      "fontName": "tahoma",
      "font": "150px"
    }
  }
}]
objs.forEach(function(Object) {
  var Elm = document.createElement('text');
  Elm.textContent = Element.Object + ' ' + Element.code.hex;
  Elm.style.backgroundColor = Element.code.hex;
  document.getElementById('ColorArea').appendChild(Elm);
});

Sure, but you need fix the JSON so its an array of objects not an array of one object which has the same key for Object.当然,但是您需要修复 JSON 以便它的对象数组不是一个 object 的数组,它与 Object 具有相同的密钥。

Then remove the space in Object , and then in change Object to something else, like item forEach(function(item) { then its not Element but item .然后删除Object中的空格,然后将 Object 更改为其他内容,例如 item forEach(function(item) {然后它不是Element而是item

I have no idea what Element.code.hex is so removed it.我不知道Element.code.hex是什么所以删除它。

 var objs = [{ "Object": { "ID": 1, "type": "button", "color": "red", "Text": "I Love", "width": "100px", "height": "100px", "Font": { "fontName": "tahoma", "font": "150px" } } }, { "Object": { "ID": 2, "type": "button", "color": "green", "Text": "I Hate", "width": "100px", "height": "100px", "Font": { "fontName": "tahoma", "font": "150px" } } } ] objs.forEach(function(item) { var Elm = document.createElement(item.Object.type); Elm.textContent = item.Object.Text; Elm.style.backgroundColor = item.Object.color; // apply other props //... document.getElementById('ColorArea').appendChild(Elm); });
 <div id="ColorArea"></div>

Note, having it be item.Object is not needed, you could simply use an array which contains the objects, no need to nest it further inside Object请注意,不需要它是item.Object ,您可以简单地使用包含对象的数组,无需将其进一步嵌套在 Object

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM