R中出现意外字符json错误 [英] Unexpected character json error in r

查看:239
本文介绍了R中出现意外字符json错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我收到此代码的以下错误消息,该代码旨在将Instagram帖子读入r.其他代码已在前20个帖子中成功读取,但是Instagram希望您请求"next_url"以获取其他帖子块.我看过对此错误的类似答案,但在我的代码中找不到错字.确实想知道是否可能是来自API的某些东西需要调整.我是R的新手,希望我能适当地解释一下.我将不胜感激.

Error in fromJSON(getURL(paste(next_url, token, sep = "")), unexpected.escape = "keep") : 
  unexpected character: p
In addition: Warning message:
In fromJSON(getURL(paste(next_url, token, sep = "")), unexpected.escape = "keep") :
  unexpected escaped character '\ ' at pos 26. Keeping value.

  username <- "XXXX"
    if(grepl(received_profile$username,username))
  {
    user_id <- received_profile$id
    media <- fromJSON(getURL(paste(next_url,token,sep="")),unexpected.escape = "keep")
    df1 = data.frame(no = 1:length(media$data))

    for(i in 1:length(media$data))
    {
      #comments
      df1$comments[i] <-media$data[[i]]$comments$count

      #likes:
      df1$likes[i] <- media$data[[i]]$likes$count

      #date
      df1$date[i] <- toString(as.POSIXct(as.numeric(media$data[[i]]$created_time), origin="1970-01-01"))
    }
    df <- rbind(df, df1)    
  }else
  {
    print("Error: User not found!")
  }

解决方案

我可以通过使用从Instagram返回的JSON响应中的pagination对象中提取next_url的代码扩展代码段来重现您的问题.

使用rjson::fromJSON解析JSON响应时,出现类似

的错误

Error in rjson::fromJSON(js) : unexpected character: i

但是,使用RJSONIO::fromJSON来解析完全相同的字符串就可以了!此外,该字符串还可以使用在线JSON验证程序验证为正确的JSON.

这强烈表明rjson::fromJSON中的错误.您现在可能要改用RJSONIO软件包.

不幸的是,这些Instagram回复很大.我将查看是否可以将JSON输出简化为可管理的内容,以便在此处发布详细信息. (我试图避免链接到外部站点上的数据.)

我怀疑Instagram用户名存在编码问题.

I'm getting the following error message for this code intended to read Instagram posts into r. Other code is successfully reading in the first 20 posts, but Instagram wants you to request the "next_url" to get additional blocks of posts. I looked at similar answers to this error, but can't find the typo in my code. Did wonder if it could be something coming from the API that needs to be adjusted. I'm new to R and hope I explained this appropriately. I would greatly appreciate any assistance.

Error in fromJSON(getURL(paste(next_url, token, sep = "")), unexpected.escape = "keep") : 
  unexpected character: p
In addition: Warning message:
In fromJSON(getURL(paste(next_url, token, sep = "")), unexpected.escape = "keep") :
  unexpected escaped character '\ ' at pos 26. Keeping value.

  username <- "XXXX"
    if(grepl(received_profile$username,username))
  {
    user_id <- received_profile$id
    media <- fromJSON(getURL(paste(next_url,token,sep="")),unexpected.escape = "keep")
    df1 = data.frame(no = 1:length(media$data))

    for(i in 1:length(media$data))
    {
      #comments
      df1$comments[i] <-media$data[[i]]$comments$count

      #likes:
      df1$likes[i] <- media$data[[i]]$likes$count

      #date
      df1$date[i] <- toString(as.POSIXct(as.numeric(media$data[[i]]$created_time), origin="1970-01-01"))
    }
    df <- rbind(df, df1)    
  }else
  {
    print("Error: User not found!")
  }

解决方案

I can reproduce your problem by extending your code snippet with code that extracts next_url from the pagination object in the JSON response returned by Instagram.

When using rjson::fromJSON to parse the JSON response I get errors like

Error in rjson::fromJSON(js) : unexpected character: i

However, using RJSONIO::fromJSON for parsing the exact same string works just fine! Also the string validates as correct JSON with online JSON validators.

This strongly points to a bug in rjson::fromJSON. You may want to use the RJSONIO package instead for now.

Unfortunately these Instagram replies are quite large. I'll see if I can strip down the JSON output to something manageable so I can post details here. (I'm trying to avoid having to link to data on an external site.)

I suspect an encoding issue with Instagram user names.

这篇关于R中出现意外字符json错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆