我正在尝试使用以下代码行解析此JSON数据:到R:为什么这不返回任何JSON?
我遇到错误
fromJSON中的错误(file =“http://dcaps-staging.media.mit.edu:8080/api/reality_analysis/get_funf_sensor_data?bearer_token=05abc49652&probe=edu.mit.media.funf.probe.builtin。 LocationProbe“): 意外字符'<'
获取/解析JSON的代码
library(RJSONIO)
json=fromJSON(file='http://dcaps-staging.media.mit.edu:8080/api/reality_analysis/get_funf_sensor_data?bearer_token=05abc49652&probe=edu.mit.media.funf.probe.builtin.LocationProbe')
json$result
JSON DATA托管其他地方
{"result": [{"TIMESTAMP": 1341888374, "_id": {"$oid": "4ffbc272f679267b6e000002"}, "LOCATION": {"mHasAccuracy": true, "mHasAltitude": false, "mProvider": "network", "mLatitude": 42.36119137, "mResults": [0.0, 0.0], "mTime": 1341888374725, "mLat1": 0.0, "mDistance": 0.0, "mAltitude": 0.0, "mLat2": 0.0, "mExtras": {"networkLocationType": "wifi", "networkLocationSource": "cached"}, "mSpeed": 0.0, "mLon1": 0.0, "mAccuracy": 32.0, "mBearing": 0.0, "mInitialBearing": 0.0, "mHasSpeed": false, "mLon2": 0.0, "mHasBearing": false, "mLongitude": -71.08727085000001}, "PROBE": "edu.mit.media.funf.probe.builtin.LocationProbe"}, {"TIMESTAMP": 1341888374, "_id": {"$oid": "4ffbe4faf679261a4a000002"}, "LOCATION": {"mHasAccuracy": true, "mHasAltitude": false, "mProvider": "network", "mLatitude": 42.36119137, "mResults": [0.0, 0.0], "mTime": 1341888374725, "mLat1": 0.0, "mDistance": 0.0, "mAltitude": 0.0, "mLat2": 0.0, "mExtras": {"networkLocationType": "wifi", "networkLocationSource": "cached"}, "mSpeed": 0.0, "mLon1": 0.0, "mAccuracy": 32.0, "mBearing": 0.0, "mInitialBearing": 0.0, "mHasSpeed": false, "mLon2": 0.0, "mHasBearing": false, "mLongitude": -71.08727085000001}, "PROBE": "edu.mit.media.funf.probe.builtin.LocationProbe"}, {"TIMESTAMP": 1341908197, "_id": {"$oid": "4ffc00f7f6792624be000003"}, "LOCATION": {"mHasAccuracy": true, "mHasAltitude": false, "mProvider": "network", "mLatitude": 42.36118479999999, "mResults": [0.0, 0.0], "mTime": 1341908197819, "mLat1": 0.0, "mDistance": 0.0, "mAltitude": 0.0, "mLat2": 0.0, "mExtras": {"networkLocationType": "wifi", "networkLocationSource": "cached"}, "mSpeed": 0.0, "mLon1": 0.0, "mAccuracy": 50.0, "mBearing": 0.0, "mInitialBearing": 0.0, "mHasSpeed": false, "mLon2": 0.0, "mHasBearing": false, "mLongitude": -71.087435429999999}, "PROBE": "edu.mit.media.funf.probe.builtin.LocationProbe"}, {"TIMESTAMP": 1341907968, "_id": {"$oid": "4ffc00f8f6792624ba000003"}, "LOCATION": {"mHasAccuracy": true, "mHasAltitude": false, "mProvider": "network", "mLatitude": 42.361219770000005, "mResults": [0.0, 0.0], "mTime": 1341907968890, "mLat1": 0.0, "mDistance": 0.0, "mAltitude": 0.0, "mLat2": 0.0, "mExtras": {"networkLocationType": "wifi", "networkLocationSource": "cached"}, "mSpeed": 0.0, "mLon1": 0.0, "mAccuracy": 50.0, "mBearing": 0.0, "mInitialBearing": 0.0, "mHasSpeed": false, "mLon2": 0.0, "mHasBearing": false, "mLongitude": -71.087460910000004}, "PROBE": "edu.mit.media.funf.probe.builtin.LocationProbe"}, {"TIMESTAMP": 1341907518, "_id": {"$oid": "4ffc0105f6792624bb000003"}, "LOCATION": {"mHasAccuracy": true, "mHasAltitude": false, "mProvider": "network", "mLatitude": 42.361184690000002, "mResults": [0.0, 0.0], "mTime": 1341907518554, "mLat1": 0.0, "mDistance": 0.0, "mAltitude": 0.0, "mLat2": 0.0, "mExtras": {"networkLocationType": "wifi", "networkLocationSource": "cached"}, "mSpeed": 0.0, "mLon1": 0.0, "mAccuracy": 50.0, "mBearing": 0.0, "mInitialBearing": 0.0, "mHasSpeed": false, "mLon2": 0.0, "mHasBearing": false, "mLongitude": -71.087443710000002}, "PROBE": "edu.mit.media.funf.probe.builtin.LocationProbe"}, {"TIMESTAMP": 1341907563, "_id": {"$oid": "4ffc0107f6792625bc000002"}, "LOCATION": {"mHasAccuracy": true, "mHasAltitude": false, "mProvider": "network", "mLatitude": 42.361189530000004, "mResults": [0.0, 0.0], "mTime": 1341907563782, "mLat1": 0.0, "mDistance": 0.0, "mAltitude": 0.0, "mLat2": 0.0, "mExtras": {"networkLocationType": "wifi", "networkLocationSource": "cached"}, "mSpeed": 0.0, "mLon1": 0.0, "mAccuracy": 50.0, "mBearing": 0.0, "mInitialBearing": 0.0, "mHasSpeed": false, "mLon2": 0.0, "mHasBearing": false, "mLongitude": -71.087443869999987}, "PROBE": "edu.mit.media.funf.probe.builtin.LocationProbe"}, {"TIMESTAMP": 1341907609, "_id": {"$oid": "4ffc0107f6792625bc000007"}, "LOCATION": {"mHasAccuracy": true, "mHasAltitude": false, "mProvider": "network", "mLatitude": 42.361184690000002, "mResults": [0.0, 0.0], "mTime": 1341907609009, "mLat1": 0.0, "mDistance": 0.0, "mAltitude": 0.0, "mLat2": 0.0, "mExtras": {"networkLocationType": "wifi", "networkLocationSource": "cached"}, "mSpeed": 0.0, "mLon1": 0.0, "mAccuracy": 50.0, "mBearing": 0.0, "mInitialBearing": 0.0, "mHasSpeed": false, "mLon2": 0.0, "mHasBearing": false, "mLongitude": -71.087443710000002}, "PROBE": "edu.mit.media.funf.probe.builtin.LocationProbe"}, {"TIMESTAMP": 1341907657, "_id": {"$oid": "4ffc0107f6792625bc00000a"}, "LOCATION": {"mHasAccuracy": true, "mHasAltitude": false, "mProvider": "network", "mLatitude": 42.361184690000002, "mResults": [0.0, 0.0], "mTime": 1341907657426, "mLat1": 0.0, "mDistance": 0.0, "mAltitude": 0.0, "mLat2": 0.0, "mExtras": {"networkLocationType": "wifi", "networkLocationSource": "cached"}, "mSpeed": 0.0, "mLon1": 0.0, "mAccuracy": 50.0, "mBearing": 0.0, "mInitialBearing": 0.0, "mHasSpeed": false, "mLon2": 0.0, "mHasBearing": false, "mLongitude": -71.087443710000002}, "PROBE": "edu.mit.media.funf.probe.builtin.LocationProbe"}, {"TIMESTAMP": 1341908149, "_id": {"$oid": "4ffc015bf6792624ba00000f"}, "LOCATION": {"mHasAccuracy": true, "mHasAltitude": false, "mProvider": "network", "mLatitude": 42.36118479999999, "mResults": [0.0, 0.0], "mTime": 1341908149220, "mLat1": 0.0, "mDistance": 0.0, "mAltitude": 0.0, "mLat2": 0.0, "mExtras": {"networkLocationType": "wifi", "networkLocationSource": "cached"}, "mSpeed": 0.0, "mLon1": 0.0, "mAccuracy": 50.0, "mBearing": 0.0, "mInitialBearing": 0.0, "mHasSpeed": false, "mLon2": 0.0, "mHasBearing": false, "mLongitude": -71.087435429999999}, "PROBE": "edu.mit.media.funf.probe.builtin.LocationProbe"}, {"TIMESTAMP": 1341907792, "_id": {"$oid": "4ffc015ef6792625bb00000c"}, "LOCATION": {"mHasAccuracy": true, "mHasAltitude": false, "mProvider": "network", "mLatitude": 42.361222089999998, "mResults": [0.0, 0.0], "mTime": 1341907792521, "mLat1": 0.0, "mDistance": 0.0, "mAltitude": 0.0, "mLat2": 0.0, "mExtras": {"networkLocationType": "wifi", "networkLocationSource": "cached"}, "mSpeed": 0.0, "mLon1": 0.0, "mAccuracy": 50.0, "mBearing": 0.0, "mInitialBearing": 0.0, "mHasSpeed": false, "mLon2": 0.0, "mHasBearing": false, "mLongitude": -71.087374009999991}, "PROBE": "edu.mit.media.funf.probe.builtin.LocationProbe"}, {"TIMESTAMP": 1341907834, "_id": {"$oid": "4ffc015ef6792625bb00000f"}, "LOCATION": {"mHasAccuracy": true, "mHasAltitude": false, "mProvider": "network", "mLatitude": 42.361197509999997, "mResults": [0.0, 0.0], "mTime": 1341907834072, "mLat1": 0.0, "mDistance": 0.0, "mAltitude": 0.0, "mLat2": 0.0, "mExtras": {"networkLocationType": "wifi", "networkLocationSource": "cached"}, "mSpeed": 0.0, "mLon1": 0.0, "mAccuracy": 50.0, "mBearing": 0.0, "mInitialBearing": 0.0, "mHasSpeed": false, "mLon2": 0.0, "mHasBearing": false, "mLongitude": -71.087434249999987}, "PROBE": "edu.mit.media.funf.probe.builtin.LocationProbe"}, {"TIMESTAMP": 1341907879, "_id": {"$oid": "4ffc015ef6792625bb000014"}, "LOCATION": {"mHasAccuracy": true, "mHasAltitude": false, "mProvider": "network", "mLatitude": 42.361229649999999, "mResults": [0.0, 0.0], "mTime": 1341907879514, "mLat1": 0.0, "mDistance": 0.0, "mAltitude": 0.0, "mLat2": 0.0, "mExtras": {"networkLocationType": "wifi", "networkLocationSource": "cached"}, "mSpeed": 0.0, "mLon1": 0.0, "mAccuracy": 50.0, "mBearing": 0.0, "mInitialBearing": 0.0, "mHasSpeed": false, "mLon2": 0.0, "mHasBearing": false, "mLongitude": -71.087405360000005}, "PROBE": "edu.mit.media.funf.probe.builtin.LocationProbe"}, {"TIMESTAMP": 1341907927, "_id": {"$oid": "4ffc015ef6792625bb00001a"}, "LOCATION": {"mHasAccuracy": true, "mHasAltitude": false, "mProvider": "network", "mLatitude": 42.361229649999999, "mResults": [0.0, 0.0], "mTime": 1341907927603, "mLat1": 0.0, "mDistance": 0.0, "mAltitude": 0.0, "mLat2": 0.0, "mExtras": {"networkLocationType": "wifi", "networkLocationSource": "cached"}, "mSpeed": 0.0, "mLon1": 0.0, "mAccuracy": 50.0, "mBearing": 0.0, "mInitialBearing": 0.0, "mHasSpeed": false, "mLon2": 0.0, "mHasBearing": false, "mLongitude": -71.087405360000005}, "PROBE": "edu.mit.media.funf.probe.builtin.LocationProbe"}, {"TIMESTAMP": 1341888374, "_id": {"$oid": "4ffc016ff679263192000002"}, "LOCATION": {"mHasAccuracy": true, "mHasAltitude": false, "mProvider": "network", "mLatitude": 42.36119137, "mResults": [0.0, 0.0], "mTime": 1341888374725, "mLat1": 0.0, "mDistance": 0.0, "mAltitude": 0.0, "mLat2": 0.0, "mExtras": {"networkLocationType": "wifi", "networkLocationSource": "cached"}, "mSpeed": 0.0, "mLon1": 0.0, "mAccuracy": 32.0, "mBearing": 0.0, "mInitialBearing": 0.0, "mHasSpeed": false, "mLon2": 0.0, "mHasBearing": false, "mLongitude": -71.08727085000001}, "PROBE": "edu.mit.media.funf.probe.builtin.LocationProbe"}, {"TIMESTAMP": 1341908013, "_id": {"$oid": "4ffc0175f67926256c000022"}, "LOCATION": {"mHasAccuracy": true, "mHasAltitude": false, "mProvider": "network", "mLatitude": 42.361192669999994, "mResults": [0.0, 0.0], "mTime": 1341908013936, "mLat1": 0.0, "mDistance": 0.0, "mAltitude": 0.0, "mLat2": 0.0, "mExtras": {"networkLocationType": "wifi", "networkLocationSource": "cached"}, "mSpeed": 0.0, "mLon1": 0.0, "mAccuracy": 50.0, "mBearing": 0.0, "mInitialBearing": 0.0, "mHasSpeed": false, "mLon2": 0.0, "mHasBearing": false, "mLongitude": -71.087434090000002}, "PROBE": "edu.mit.media.funf.probe.builtin.LocationProbe"}, {"TIMESTAMP": 1341908058, "_id": {"$oid": "4ffc0175f67926256c000025"}, "LOCATION": {"mHasAccuracy": true, "mHasAltitude": false, "mProvider": "network", "mLatitude": 42.361204739999991, "mResults": [0.0, 0.0], "mTime": 1341908058976, "mLat1": 0.0, "mDistance": 0.0, "mAltitude": 0.0, "mLat2": 0.0, "mExtras": {"networkLocationType": "wifi", "networkLocationSource": "cached"}, "mSpeed": 0.0, "mLon1": 0.0, "mAccuracy": 50.0, "mBearing": 0.0, "mInitialBearing": 0.0, "mHasSpeed": false, "mLon2": 0.0, "mHasBearing": false, "mLongitude": -71.087433469999993}, "PROBE": "edu.mit.media.funf.probe.builtin.LocationProbe"}, {"TIMESTAMP": 1341908104, "_id": {"$oid": "4ffc0175f67926256c00002a"}, "LOCATION": {"mHasAccuracy": true, "mHasAltitude": false, "mProvider": "network", "mLatitude": 42.361204739999991, "mResults": [0.0, 0.0], "mTime": 1341908104005, "mLat1": 0.0, "mDistance": 0.0, "mAltitude": 0.0, "mLat2": 0.0, "mExtras": {"networkLocationType": "wifi", "networkLocationSource": "cached"}, "mSpeed": 0.0, "mLon1": 0.0, "mAccuracy": 50.0, "mBearing": 0.0, "mInitialBearing": 0.0, "mHasSpeed": false, "mLon2": 0.0, "mHasBearing": false, "mLongitude": -71.087433469999993}, "PROBE": "edu.mit.media.funf.probe.builtin.LocationProbe"}, {"TIMESTAMP": 1341907026, "_id": {"$oid": "4ffc017bf6792624bc000007"}, "LOCATION": {"mHasAccuracy": true, "mHasAltitude": false, "mProvider": "network", "mLatitude": 42.361204739999991, "mResults": [0.0, 0.0], "mTime": 1341907026801, "mLat1": 0.0, "mDistance": 0.0, "mAltitude": 0.0, "mLat2": 0.0, "mExtras": {"networkLocationType": "wifi", "networkLocationSource": "cached"}, "mSpeed": 0.0, "mLon1": 0.0, "mAccuracy": 50.0, "mBearing": 0.0, "mInitialBearing": 0.0, "mHasSpeed": false, "mLon2": 0.0, "mHasBearing": false, "mLongitude": -71.087433469999993}, "PROBE": "edu.mit.media.funf.probe.builtin.LocationProbe"}, {"TIMESTAMP": 1341907068, "_id": {"$oid": "4ffc017bf6792624bc00000b"}, "LOCATION": {"mHasAccuracy": true, "mHasAltitude": false, "mProvider": "network", "mLatitude": 42.361182479999997, "mResults": [0.0, 0.0], "mTime": 1341907068261, "mLat1": 0.0, "mDistance": 0.0, "mAltitude": 0.0, "mLat2": 0.0, "mExtras": {"networkLocationType": "wifi", "networkLocationSource": "cached"}, "mSpeed": 0.0, "mLon1": 0.0, "mAccuracy": 50.0, "mBearing": 0.0, "mInitialBearing": 0.0, "mHasSpeed": false, "mLon2": 0.0, "mHasBearing": false, "mLongitude": -71.087406810000004}, "PROBE": "edu.mit.media.funf.probe.builtin.LocationProbe"}, {"TIMESTAMP": 1341906978, "_id": {"$oid": "4ffc017bf67926312c00000b"}, "LOCATION": {"mHasAccuracy": true, "mHasAltitude": false, "mProvider": "network", "mLatitude": 42.361204739999991, "mResults": [0.0, 0.0], "mTime": 1341906978288, "mLat1": 0.0, "mDistance": 0.0, "mAltitude": 0.0, "mLat2": 0.0, "mExtras": {"networkLocationType": "wifi", "networkLocationSource": "cached"}, "mSpeed": 0.0, "mLon1": 0.0, "mAccuracy": 50.0, "mBearing": 0.0, "mInitialBearing": 0.0, "mHasSpeed": false, "mLon2": 0.0, "mHasBearing": false, "mLongitude": -71.087433469999993}, "PROBE": "edu.mit.media.funf.probe.builtin.LocationProbe"}, {"TIMESTAMP": 1341888328.0, "_id": {"$oid": "5004377f114952a0f6d415c1"}, "SCAN_RESULTS": [{"TIMESTAMP": 1316035121.0, "LOCATION": {"mHasAccuracy": true, "mHasAltitude": false, "mLon2": 0.0, "mLatitude": 42.361263270000009, "mResults": [0.0, 0.0], "mLat1": 0.0, "mDistance": 0.0, "mAltitude": 0.0, "mLat2": 0.0, "mExtras": {"networkLocationType": "wifi", "networkLocationSource": "cached"}, "mSpeed": 0.0, "mInitialBearing": 0.0, "mAccuracy": 52.0, "mBearing": 0.0, "mTime": 1316035121482.0, "mLon1": 0.0, "mHasSpeed": false, "mProvider": "network", "mHasBearing": false, "mLongitude": -71.087640010000001}}], "PROBE": "edu.mit.media.funf.probe.builtin.LocationProbe"}]}
注意 我认为问题是由JSON引起的。我能够使用上面“Code to get / parse JSON”中列出的相同方法查询其他JSON文件,但是当我尝试查询此特定文件时,我收到上述错误...
答案 0 :(得分:4)
> readLines('http://dcaps-staging.media.mit.edu:8080/api/reality_analysis/get_funf_sensor_data?bearer_token=05abc49652&probe=edu.mit.media.funf.probe.builtin.LocationProbe')
[1] "<p>Test</p>"
将此与浏览器调用相比较。因此,您没有返回任何JSON,因此错误。
以下对我来说很好:
library(RCurl)
library(RJSONIO)
data<-getURL('http://dcaps-staging.media.mit.edu:8080/api/reality_analysis/get_funf_sensor_data?bearer_token=05abc49652&probe=edu.mit.media.funf.probe.builtin.LocationProbe')
jdata<-fromJSON(data)
> jdata[[1]][[1]]
$TIMESTAMP
[1] 1341888374
$`_id`
$oid
"4ffbc272f679267b6e000002"
$LOCATION
$LOCATION$mHasAccuracy
[1] TRUE
etc...
原因可能是提供JSON的非标准端口(8080)。如果您使用浏览器Call to port 80,则会得到相同的<p>Test</p>
readLines
次。因此,默认的R方法可能无法处理端口更改。修复是使用RCurl似乎处理它。