简体   繁体   English

如何使用 JavaScript 读取没有父根名称的 JSON?

[英]How can I read a JSON without parent root name with JavaScript?

I have a JSON file which has this:我有一个 JSON 文件,其中包含:

[
   {
      "id":"1",
      "customer":{
         "nickname":"nick001",
         "email":"",
         "id":"15615",
         "name":"Smith Dole",
         "phone":"5555555"
      },
      "totalorder":"44155",
      "items":[
         {
            "code":"041545420",
            "title":"item",
            "quantity":1,
            "price":"2461.7500",
            "subtotal":2461.75
         }
      ]
   }
]

As you can see it doesn´t have any parent root name.如您所见,它没有任何父根名称。 How can I read this JSON with JavaScript?如何使用 JavaScript 阅读此 JSON?

I was thinking to PARSE the Json but JSON.parse(need the name)我正在考虑解析 Json 但 JSON.parse(需要名称)

Is it possible to call this JSON in my JavaScript code and asign it a variable?是否可以在我的 JavaScript 代码中调用此 JSON 并将其分配给变量?

var newSelling = data from the JSON var newSelling = 来自 JSON 的数据

The JSON is being generated by another system which generates it that way so I need to read the JSON to get the data needed for other processes JSON 是由另一个系统生成的,它以这种方式生成,所以我需要读取 JSON 以获取其他进程所需的数据

the JSON is automatically generated and it is in an external file JSON 是自动生成的,它在一个外部文件中

So it is a JSON file.所以它是一个 JSON 文件。 Request it with fetch or XMLHttpRequest and access the JSON.通过 fetch 或 XMLHttpRequest 请求它并访问 JSON。

fetch('/path/to/your/file.json')
  .then(response => response.json())
  .then(data => {
    console.log(data)
  });

If you want to read the file in the Browser from your local you can use one of these solutions:如果要从本地读取浏览器中的文件,可以使用以下解决方案之一:

I recommend Fetch API.我推荐获取 API。

      fetch("data.json")
      .then(response => response.json())
      .then(json => console.log(json));

It works in Firefox, but in Chrome you have to customize security setting.它适用于 Firefox,但在 Chrome 中您必须自定义安全设置。

Parsing JSON should work as follows:解析 JSON 应该如下工作:

const json = '{"result":true, "count":42}';
const obj = JSON.parse(json);

console.log(obj.count);
// expected output: 42

console.log(obj.result);
// expected output: true

More info 更多信息

Edit: You can red this question .编辑:你可以红色这个问题 I think it may help我认为这可能会有所帮助

If you look carefully, you'll see that this JSON file is just a JSON array with only one Object inside of it.如果你仔细看,你会发现这个 JSON 文件只是一个 JSON 数组,里面只有一个 Object。 So you have to access the object that is in the array.因此,您必须访问阵列中的 object。

let arr = JSON.parse(*the json object*);
let obj = arr[0];

I haven't tested this, but hopefully this helps.我没有对此进行测试,但希望这会有所帮助。

Here in your case, using JSON.parse only is not working but the combination of JSON.stringify and JSON.parse is working as I tried.在您的情况下,仅使用 JSON.parse 不起作用,但JSON.stringifyJSON.parse的组合正如我所尝试的那样工作。 So first, we can stringify it and then we can parse it所以首先,我们可以将它字符串化,然后我们可以解析它

Like this:像这样:

var a = [
   {
      "id":"1",
      "customer":{
         "nickname":"nick001",
         "email":"",
         "id":"15615",
         "name":"Smith Dole",
         "phone":"5555555"
      },
      "totalorder":"44155",
      "items":[
         {
            "code":"041545420",
            "title":"item",
            "quantity":1,
            "price":"2461.7500",
            "subtotal":2461.75
         }
      ]
   }
];

var b = JSON.stringify(a);
var c = JSON.parse(b);
console.log(c[0].id); //output : 1

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

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