我有一个具有以下内容的json对象:
{
"similarUserScores":[
{
"user":"u26",
"score":0.6199970840986468
},
{
"user":"u37",
"score":0.5405403752816058
},
{
"user":"u12",
"score":0.523836143999991
},
{
"user":"u24",
"score":0.4990480549411648
}
]
}
我需要将其转换为Java对象。所以我创建了一个Java类来映射它:
public class SimilarUserScores {
private String user;
private double score;
public String getUser() {
return user;
}
public void setUser(String user) {
this.user = user;
}
public double getScore() {
return score;
}
public void setScore(double score) {
this.score = score;
}
}
那么如何将上述JSON转换为Java对象列表?
//Get Recommendations for this user
JsonObject recommendedUserJson = PredictionIoClientHelper
.getInstance().getRecommendedUser("u"+userId, limit);
if(recommendedUserJson != null) {
Gson gson = new Gson();
String jsonString = recommendedUserJson.toString();
SimilarUserScores[] recommendedUsers= gson.fromJson(jsonString, SimilarUserScores[].class);
}
我尝试使用上面的代码段,但是没有用。
富国沪深
HUX布斯
相关分类