简体   繁体   English

从api属性接收的JSON对象返回未定义

[英]JSON object recieved from api property returns undefined

For the project that I am working on, I am using the Shopify API which allows you to retrieve products and other information from your store to be retrieved in the format of a JSON object. 对于我正在处理的项目,我正在使用Shopify API,该API允许您从商店中检索产品和其他信息,并以JSON对象的格式进行检索。 I was able to successfully get the JSON object from the API, however when I try to access a property of the JSON object, it returns undefined. 我能够从API成功获取JSON对象,但是当我尝试访问JSON对象的属性时,它返回未定义。 I have looked at a couple of articles that I will refrence below, but the problem for those users were things such as needing to use: 我看了几篇我将在下面引用的文章,但是这些用户遇到的问题是诸如需要使用的东西:

JSON.parse()

for a JSON object enclosed by strings which is not my probelem, I have tried a few other work arounds as well but with no luck, I originally thought that the problem was that my code needed to use an "Async/Await" function in order to wait for a response from the API, but I then realized that wouldn't make sense considering I can recieve the whole JSON object itself with no problems. 对于用字符串括起来的JSON对象(不是我的探针),我也尝试了其他一些解决方法,但是没有运气,我本来以为问题是我的代码需要使用“异步/等待”功能等待来自API的响应,但后来我意识到考虑到我可以毫无问题地接收整个JSON对象本身,这毫无意义。

When I use: 当我使用时:

request.get(url, {headers})
.then( result => {
console.log(result); // Only accessing object itself
});

I recieve the JSON object response correctly with no error like this: 我正确接收了JSON对象响应,没有这样的错误:

     {"products":[{"title":"Test Product 1","body_html":"This is a product that is being tested for retrieval!",
"product_type":"","created_at":"2018-08-21T17:49:07-07:00","handle":"test-product-1","updated_at":"2018-08-21T17:49:07-07:00","published_at":"2018-08-21T17:48:19-07:00","template_suffix":null,"tags":"",
"published_scope":"web","variants":[{"title":"Default Title","price":"5.00","sku":"","position":1,"inventory_policy":"deny",
"compare_at_price":null,"fulfillment_service":"manual","inventory_management":null,"option1":"Default Title","option2":null,"option3":null,
"created_at":"2018-08-21T17:49:07-07:00","updated_at":"2018-08-21T17:49:07-07:00","taxable":true,"barcode":"",
"grams":99790,"image_id":null,"inventory_quantity":1,"weight":220.0,"weight_unit":"lb","old_inventory_quantity":1,
"requires_shipping":true,}],"options":[{"name":"Title","position":1,"values":["Default Title"]}],"images":[],"image":null}]}

However when I use this, the JSON object property returns undefined: 但是,当我使用它时,JSON对象属性返回未定义:

request.get(url, {headers})
    .then( result => {
    console.log(result.products[0]); // Accessing the first item in JSON "products" array
    });

Articles I have already checked out: 我已经签出的文章:

cannot access json object property returns undefined 无法访问json对象属性返回未定义

JSON object returns undefined value JSON对象返回未定义的值

JSON objects returns undefined JSON对象返回未定义

Would anyone be able to explain my error or why this is happening? 任何人都可以解释我的错误,或者为什么会这样? I am more than happy to edit my question to include any code/information that might be helpful. 我很高兴编辑我的问题,以包括任何可能有用的代码/信息。

Thanks in advance, Michael 预先感谢迈克尔

try this: 尝试这个:

console.log("data:", JSON.stringify(result.products[0], null, 2));

console.log to print the result to your console. console.log将结果打印到控制台。 Use Chrome and developer tools and you will see a console option - excellent for debugging. 使用Chrome和开发人员工具,您将看到一个控制台选项-非常适合调试。

JSON.stringify turns the JSON data into something you can see and read. JSON.stringify将JSON数据转换为您可以查看和阅读的内容。 You can convert the data and then split as you need this way too. 您也可以转换数据,然后根据需要拆分。

OK, a second answer. 好,第二个答案。 I can't check this, as I don't have your JSON data, however, I would try something that would likely resemble this... 我无法检查此内容,因为我没有您的JSON数据,但是,我会尝试一些可能与此类似的内容...

data.Items[0].field data.Items [0]点域

if the data is not correctly formatted then take the stringify approach and split it out. 如果数据格式不正确,则采用字符串化方法并将其拆分。 Otherwise, consider this: 否则,请考虑以下事项:

products":[{"title":"Test Product 1"

variable = products[0].title;

I would tend to use a function to pull all the data out in one shot. 我倾向于使用一种功能来一次性提取所有数据。

function Getv(data){    global.myApp.v = JSON.stringify(data, null, 2); }

then I might run this... 那我可以运行这个...

 Getv(data.Items[0]); let splitData = global.myApp.v.split('\"'); let vCount= splitData.length; 

if the object returns like this it wouldn't work because it's missing a colon after "products" identifier. 如果对象返回这样的内容,将无法使用,因为在“产品”标识符之后缺少冒号。 like Luca said it's not a valid JSON response 就像卢卡说的那样,这不是有效的JSON响应

Try this: 尝试这个:

var resultJson = JSON.parse(result);

console.log(resultJson.products[0].varname);

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

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