我正在编写代码以从Web API获取JSON数据,并最终希望使用 let imageCache = NSCache<AnyObject, AnyObject>()
imageCache.setObject(imageToCache!, forKey: urlString as AnyObject)
if let imageFromCache = imageCache.object(forKey: urlString as AnyObject) as? UIImage {
self.image = imageFromCache
return
}
将其放入HBASE
表中。
我用于从Web API获取数据的初始代码以字符串形式返回我的数据:
Phoenix
现在,我想将具有内置JSON值的字符串转换为适当的JSON对象,以便可以将其放入class Health() {
def getHealthData() {
val userRDD = df.select("ID", "PERMISSION_T", "UPDATE_T", "HEALTHTID").rdd;
val newData = userRDD.map(
(
row => {
{
var ID = row(0).toString // User ID
var permT = row(1).toString // access token
var rT = row(2).toString // refresh token
var healthID = row(3).toString // fitbit user id
var forDate = "2018-12-04"
val hUrl = s "web api url"
try {
--Piece of code to connect to web api-- --
hJsonStr = heartHandler.handleResponse(heartResponse).trim
println(s "=============In TRY(Print hJsonStr) ============$hJsonStr")
}
heartClient.getConnectionManager().shutdown();
} catch {
case unauthorized:
Exception => {
println(s "Unknown exception: $hUrl")
RecoverToken()
}
}
def RecoverToken(): Unit = {
println("<<<<<<<<<< Recover Token >>>>>>>>>")
val recoverUrl = "recover url"
}
//heartJson
//heartJsonStr
"ID:" + ID + s "--$hUrl -- $hJsonStr -- " //+ accessToken //+ "--" + activitiesJsonStr
};
}
)
);
newData.collect.foreach {
println
}
val fb = new Fitbit(); // initialize new fitbit object
fb.getFitBitData(); // call function to fetch data
中。我需要在Zappelin上使用Spark,Scala做到这一点。
以下是我使用上述代码从Web API获取的示例数据:
ID:XYZ1--网址- {“ activities-heart”:[{“ dateTime”:“ 2018-12-18”,“ value”:{“ customHeartRateZones”:[],“ heartRateZones”:[{“ caloriesOut”:714.31496,“ max”:88 ,“ min”:30,“ minutes”:667,“ name”:“ Out 的 范围“},{” caloriesOut“:240.01076,” max“:123,” min“:88,” minutes“:66,” name“:” Fat Burn“},{” caloriesOut“:0,” max“:150,” min“:123,” minutes“:4,” name“:” Cardio“},{” caloriesOut“:0,” max“:220 ,“ min”:150,“ minutes”:0,“ name”:“ Peak”}],“ restingHeartRate”:62}}]} -ID:XYZ2--网络网址--
我尝试导入几个HBASE
库,并使用了以下代码,但由于语法错误而失败:
JSON
答案 0 :(得分:1)
在@vindev的建议下,我显然意识到我在尝试将JSON字符串转换为JSON对象之前需要删除其他字符串。一旦做到这一点,我就可以在循环内将其转换。在代码中获得hJsonStr
的值后,我使用了下面的代码:
hJsonStr = heartHandler.handleResponse(heartResponse).trim
import org.json4s.jackson.JsonMethods._
val parsedHJsonStr = parse(hJsonStr)