gpt4 book ai didi

json - 使用 R 解析 JSONP 文件

转载 作者:行者123 更新时间:2023-12-04 18:29:11 24 4
gpt4 key购买 nike

这里是 JSON 新手。您能帮忙使用 R 解析 JSON 文件吗?我确实尝试过 jsonlite 和 rjson,但不断出现错误。

以下是通过 api 检索的数据。

data <- GET("http://svcs.ebay.com/services/search/FindingService/v1?OPERATION-NAME=findItemsByKeywords&SERVICE-VERSION=1.0.0&SECURITY-APPNAME=GLOBAL-ID=EBAY-US&RESPONSE-DATA-FORMAT=JSON&callback=_cb_findItemsByKeywords&REST-PAYLOAD&keywords=harry%20potter&paginationInput.entriesPerPage=10")

JSON 看起来像这样:

/**/_cb_findItemsByKeywords({
"findItemsByKeywordsResponse":[
{
"ack":[
"Success"
],
"version":[
"1.13.0"
],
"timestamp":[
"2016-01-29T16:36:25.984Z"
],
"searchResult":[
{
"@count":"1",
"item":[
{
"itemId":[
"371533364795"
],
"title":[
"Harry Potter: Complete 8-Film Collection (DVD, 2011, 8-Disc Set)"
],
"globalId":[
"EBAY-US"
],
"primaryCategory":[
{
"categoryId":[
"617"
],
"categoryName":[
"DVDs & Blu-ray Discs"
]
}
],
"galleryURL":[
"http:\/\/thumbs4.ebaystatic.com\/m\/mn5Agt0HFD89L7_-lqfrZZw\/140.jpg"
],
"viewItemURL":[
"http:\/\/www.ebay.com\/itm\/Harry-Potter-Complete-8-Film-Collection-DVD-2011-8-Disc-Set-\/371533364795"
],
"productId":[
{
"@type":"ReferenceID",
"__value__":"110258144"
}
],
"paymentMethod":[
"PayPal"
],
"autoPay":[
"false"
],
"postalCode":[
"60131"
],
"location":[
"Franklin Park,IL,USA"
],
"country":[
"US"
],
"shippingInfo":[
{
"shippingServiceCost":[
{
"@currencyId":"USD",
"__value__":"0.0"
}
],
"shippingType":[
"FlatDomesticCalculatedInternational"
],
"shipToLocations":[
"US",
"CA",
"GB",
"AU",
"AT",
"BE",
"FR",
"DE",
"IT",
"JP",
"ES",
"TW",
"NL",
"CN",
"HK",
"MX",
"DK",
"RO",
"SK",
"BG",
"CZ",
"FI",
"HU",
"LV",
"LT",
"MT",
"EE",
"GR",
"PT",
"CY",
"SI",
"SE",
"KR",
"ID",
"ZA",
"TH",
"IE",
"PL",
"RU",
"IL"
],
"expeditedShipping":[
"false"
],
"oneDayShippingAvailable":[
"false"
],
"handlingTime":[
"1"
]
}
],
"sellingStatus":[
{
"currentPrice":[
{
"@currencyId":"USD",
"__value__":"26.95"
}
],
"convertedCurrentPrice":[
{
"@currencyId":"USD",
"__value__":"26.95"
}
],
"sellingState":[
"Active"
],
"timeLeft":[
"P16DT3H12M6S"
]
}
],
"listingInfo":[
{
"bestOfferEnabled":[
"false"
],
"buyItNowAvailable":[
"false"
],
"startTime":[
"2016-01-15T19:43:31.000Z"
],
"endTime":[
"2016-02-14T19:48:31.000Z"
],
"listingType":[
"StoreInventory"
],
"gift":[
"false"
]
}
],
"returnsAccepted":[
"true"
],
"condition":[
{
"conditionId":[
"1000"
],
"conditionDisplayName":[
"Brand New"
]
}
],
"isMultiVariationListing":[
"false"
],
"topRatedListing":[
"true"
]
}
]
}
],
"paginationOutput":[
{
"pageNumber":[
"1"
],
"entriesPerPage":[
"1"
],
"totalPages":[
"138112"
],
"totalEntries":[
"138112"
]
}
],
"itemSearchURL":[
"http:\/\/www.ebay.com\/sch\/i.html?_nkw=harry+potter&_ddo=1&_ipg=1&_pgn=1"
]
}
]
})

最佳答案

问题是你的数据不是json,而是JavaScript,准确地说是jsonp。如果您只想解析 JSON 数据,则必须剥离填充回调函数。

req <- httr::GET("http://svcs.ebay.com/services/search/FindingService/v1?OPERATION-NAME=findItemsByKeywords&SERVICE-VERSION=1.0.0&SECURITY-APPNAME=YOUR-APP-123456&GLOBAL-ID=EBAY-US&RESPONSE-DATA-FORMAT=JSON&callback=_cb_findItemsByKeywords&REST-PAYLOAD&keywords=harry%20potter&paginationInput.entriesPerPage=10")
txt <- content(req, "text")
json <- sub("/**/_cb_findItemsByKeywords(", "", txt, fixed = TRUE)
json <- sub(")$", "", json)
mydata <- jsonlite::fromJSON(json)

额外功劳:或者您可以使用实际的 JavaScript 引擎来解析 JavaScript:

library(V8)
ctx <- V8::v8()
ctx$eval("var out;")
ctx$eval("function _cb_findItemsByKeywords(x){out = x;}")
ctx$source("http://svcs.ebay.com/services/search/FindingService/v1?OPERATION-NAME=findItemsByKeywords&SERVICE-VERSION=1.0.0&SECURITY-APPNAME=YOUR-APP-123456&GLOBAL-ID=EBAY-US&RESPONSE-DATA-FORMAT=JSON&callback=_cb_findItemsByKeywords&REST-PAYLOAD&keywords=harry%20potter&paginationInput.entriesPerPage=10")
mydata <- ctx$get("out")

关于json - 使用 R 解析 JSONP 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35089918/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com