简体   繁体   English

如果字段返回 TypeError,则忽略 JSON 解析错误

[英]Ignore JSON parsing error if a field returns TypeError

This feels like a totally straightforward try/catch issue, but as a total javascript noob I am clearly missing something.这感觉就像一个非常简单的 try/catch 问题,但作为一个完全的 JavaScript 菜鸟,我显然错过了一些东西。

I am trying to parse data from the Cleveland Museum of Art open collections API.我正在尝试解析来自克利夫兰艺术博物馆开放收藏 API 的数据。 Everything works fine, except that some works do not name the artists involved.一切正常,除了一些作品没有命名所涉及的艺术家。

When there is an artist included in the metadata (for example here ) this snippet effectively pulls it:当元数据中包含艺术家时(例如此处),此代码段有效地提取了它:

jsonObj['data'][0]['creators'][0]['description'] jsonObj['data'][0]['creators'][0]['description']

When there is not an artist in the metadata (for example here ), I get a当元数据中没有艺术家时(例如这里),我得到一个

TypeError: jsonObj.data[0].creators[0] is undefined类型错误:jsonObj.data[0].creators[0] 未定义

error.错误。

The internet suggests that this is a perfect time to use a try/catch statement:互联网表明这是使用 try/catch 语句的最佳时机:

var data_author = tryCatch(jsonObj['data'][0]['creators'][0]['description'])

var function_image_data = [data_author]

function tryCatch(json_address) {
    try {
         output_json_data = json_address
     }
     catch (e) {
         output_json_data = ''

     }

      return output_json_data
  }

While that works when there is an entry for the author, it appears to have no impact when there is an error.虽然这在有作者条目时有效,但在出现错误时似乎没有影响。 What incredibly obvious thing am I missing from this statement?我在这句话中遗漏了什么非常明显的东西?

Thank you!谢谢!

EDIT: Based on tex's suggestions below, I moved the attempt to get data out of the function and directly into the code.编辑:基于 tex 下面的建议,我将尝试从函数中获取数据并直接放入代码中。 I'm not entirely sure why it works, but it does avoid the possibility that things are being evaluated before they hit the function.我不完全确定它为什么起作用,但它确实避免了在事物到达函数之前被评估的可能性。 The section that searches for all of the elements of a work now looks like:搜索作品所有元素的部分现在看起来像:

    var data_tombstone = tryCatch(jsonObj['data'][0]['tombstone'])
    console.log(data_tombstone)
    var data_title = tryCatch(jsonObj['data'][0]['title'])
    try {
         data_author = jsonObj['data'][0]['creators'][0]['description']
     }
     catch (e) {
         data_author = ''

     }
    var data_creation_date = tryCatch(jsonObj['data'][0]['creation_date'])

jsonObj['data'][0]['creators'][0]['description'] is being evaluated before your tryCatch function is ever called, which means the error won't be caught. jsonObj['data'][0]['creators'][0]['description']在您的tryCatch函数被调用之前被评估,这意味着错误不会被捕获。

If you're open to using a library, I suggest Ramda .如果您愿意使用库,我建议您使用Ramda Here's a solution using Ramda (that doesn't involve try..catch :这是使用 Ramda 的解决方案(不涉及try..catch

 const works = { data: [{ creators: [{ description: 'an author'}] }] } const doesntWork = { data: [{ creators: [] }] } const getAuthor = R.pathOr('', ['data', 0, 'creators', 0, 'description']) const data_author_works = getAuthor(works) const data_author_doesnt_work = getAuthor(doesntWork) console.log('works: ', data_author_works) console.log('doesn\\'t work: ', data_author_doesnt_work)
 <script src="https://cdnjs.cloudflare.com/ajax/libs/ramda/0.26.1/ramda.min.js"></script>

If you don't want to use a library, and do want to use try...catch , you'll need to do something like this, but I wouldn't recommend this approach:如果您不想使用库,并且确实想使用try...catch ,则需要执行以下操作,但我不推荐这种方法:

 const works = { data: [{ creators: [{ description: 'an author'}] }] } const doesntWork = { data: [{ creators: [] }] } const getAuthor = jsonData => { try { return jsonData.data[0].creators[0].description } catch (e) { return '' } } const data_author_works = getAuthor(works) const data_author_doesnt_work = getAuthor(doesntWork) console.log('works: ', data_author_works) console.log('doesn\\'t work: ', data_author_doesnt_work)

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

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