我的回复来自服务器。我想用pojo类解析它。我创建了具有setter和getter方法的pojo类,它们保存了值。但是我使用的逻辑工作不正常。虽然我打印的值并没有完全返回值。
回应:
{
"code":200,
"status":"ok",
"message":"Promotions feed.",
"data":{
"feed":[
{
"id":0,
"businessId":0,
"photoUrl":"https:\/\/d1e6yi6s3cx2ur.cloudfront.net\/videos\/0\/8b84c9c1-50ed-4e93-9785-797bbf2be667.png",
"videoUrl":"https:\/\/d1e6yi6s3cx2ur.cloudfront.net\/videos\/0\/_464b2fb6-ede6-403f-b6ae-1666c9504337.mov",
"isNew":true,
"type":"news"
},
{
"id":198,
"specialType":"limited",
"name":"Big Data Shoes!",
"description":"Get them now while you can. They're dangerous!",
"start":"2017-06-04 14:59:01Z",
"end":"2018-01-26 19:00:00Z",
"limitedAmount":30,
"countInfo":{
"views":70,
"likes":0,
"liked":false,
"type":"count_info"
},
"type":"special"
},
{
"id":"1616636",
"partnerId":3,
"uuid":"57dacc36-abac-4bb3-89a4-f1981130b206",
"type":"partner_offer"
}
]
}
}
FeedPojo.java
public class FeedPojo {
private static String id;
private static String businessId;
private static String photoUrl;
private static String videoUrl;
public static String getId() {
return id;
}
public static void setId(String id) {
FeedPojo.id = id;
}
public static String getPartnerId() {
return partnerId;
}
public static void setPartnerId(String partnerId) {
FeedPojo.partnerId = partnerId;
}
}
MainActivity.java
FeedPojo.setType(promoJsonObject.getString("type"));
Log.d(TAG, "TypeFeed:" + FeedPojo.getType());
Log.e(TAG, "Type:" + promoJsonObject.getString("type"));
if (promoJsonObject.getString("type") == "news") {
FeedPojo.setId(promoJsonObject.getString("id"));
FeedPojo.setBusinessName(promoJsonObject.getString("businessName"));
FeedPojo.setBusinessLogoUrl(promoJsonObject.getString("businessLogoUrl"));
FeedPojo.setText(promoJsonObject.getString("text"));
Log.d(TAG,"Id"+FeedPojo.getId());
Log.d(TAG,"Businessname"+FeedPojo.getBusinessName());
}
if (FeedPojo.getType() == "special") {
FeedPojo.setId(promoJsonObject.getString("id"));
FeedPojo.setType(promoJsonObject.getString("type"));
FeedPojo.setName(promoJsonObject.getString("name"));
FeedPojo.setDescription(promoJsonObject.getString("description"));
FeedPojo.setStart(promoJsonObject.getString("start"));
FeedPojo.setEnd(promoJsonObject.getString("end"));
}
答案 0 :(得分:0)
我们可以像这样解析, FeedPojo.java
public class FeedParser {
public static void parse(String response) {
try {
JSONObject feedObject = new JSONObject(response);
if (feedObject == null) {
return;
}
JSONArray feedArray = feedObject.optJSONArray("feed");
if (feedArray == null) {
return;
}
ArrayList<FeedPojo> feedPojos = new ArrayList<>();
for (int i = 0; i < feedArray.length(); i++) {
JSONObject feed = feedArray.optJSONObject(i);
if (feed == null) {
continue;
}
FeedPojo feedPojo = new FeedPojo();
feedPojo.id = feed.optInt("id", 0);
feedPojo.businessId = feed.optInt("businessId", 0);
feedPojo.limitedAmount = feed.optInt("limitedAmount", 0);
feedPojo.partnerId = feed.optInt("partnerId", 0);
feedPojo.photoUrl = feed.optString("photoUrl", "");
feedPojo.type = feed.optString("type", "");
feedPojo.specialType = feed.optString("specialType", "");
feedPojo.name = feed.optString("name", "");
feedPojo.description = feed.optString("description", "");
feedPojo.start = feed.optString("start", "");
feedPojo.end = feed.optString("end", "");
feedPojo.uuid = feed.optString("uuid", "");
feedPojo.isNew = feed.optBoolean("isNew", false);
feedPojo.isNew = feed.optBoolean("isNew", false);
JSONObject countInfoObj = feed.optJSONObject("countInfo");
if (countInfoObj == null) {
feedPojos.add(feedPojo);
continue;
}
feedPojo.views = countInfoObj.optInt("views", 0);
feedPojo.likes = countInfoObj.optInt("likes", 0);
feedPojo.liked = countInfoObj.optBoolean("liked", false);
feedPojo.countInfoType = countInfoObj.optString("type", "");
feedPojos.add(feedPojo);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}
FeedParser.java
# Getting stock data
library(quantmod)
tickers <- c("ASC.OL", "AFG.OL", "AKA.OL", "AKER.OL", "AKERBP.OL", "AKSO.OL", "AKVA.OL", "AMSC.OL", "APP.OL", "AQUA.OL", "ARCHER.OL", "ARCUS.OL", "AFK.OL", "ASETEK.OL", "ATEA.OL", "ATLA-NOK.OL", "AURG.OL", "AUSS.OL", "AVANCE.OL", "AVM.OL", "AXA.OL", "B2H.OL", "BAKKA.OL", "BEL.OL", "BERGEN.OL", "BGBIO.OL", "BIOTEC.OL", "BON.OL", "BOR.OL", "BRG.OL", "BOUVET.OL", "BWLPG.OL", "BWO.OL", "BMA.OL", "COV.OL", "CXENSE.OL", "DAT.OL", "DESSC.OL", "DNB.OL", "DNO.OL", "DOF.OL", "EIOF.OL", "EKO.OL", "EMGS.OL", "EMAS.OL", "ENTRA.OL", "EPR.OL", "FAR.OL", "FOE.OL", "FRO.OL", "FUNCOM.OL", "GIG.OL", "RISH.OL", "GJF.OL", "GOGL.OL", "GOD.OL", "GSF.OL", "GYL.OL", "HNA.OL", "HNB.OL", "HAVI.OL", "HYARD.OL", "HELG.OL", "HEX.OL", "HIDDN.OL", "HLNG.OL", "HSPG.OL", "IMSK.OL", "IDEX.OL", "INC.OL", "ISSG.OL", "INSR.OL", "IOX.OL", "ITX.OL", "ITE.OL", "JIN.OL", "JAEREN.OL", "KID.OL", "KIT.OL", "KOA.OL", "KOG.OL", "KVAER.OL", "LSG.OL", "LINK.OL", "MHG.OL", "MEDI.OL", "MELG.OL", "MULTI.OL", "NAPA.OL", "NAVA.OL", "NEL.OL", "NEXT.OL", "NGT.OL", "NANO.OL", "NOD.OL", "NHY.OL", "NSG.OL", "NRS.OL", "NAS.OL", "NOR.OL", "NOFI.OL", "NPRO.OL", "NRC.OL", "NTS.OL", "OCY.OL", "OTS.OL", "ODL.OL", "ODF.OL", "ODFB.OL", "OLT.OL", "OPERA.OL", "ORK.OL", "PEN.OL", "PARB.OL", "PGS.OL", "PDR.OL", "PHO.OL", "PLCS.OL", "POL.OL", "PRS.OL", "PROTCT.OL", "QFR.OL", "QEC.OL", "RAKP.OL", "REACH.OL", "REC.OL", "RENO.OL", "SDSD.OL", "SAFE.OL", "SALM.OL", "SADG.OL", "SAS-NOK.OL", "SSO.OL", "SCHA.OL", "SCHB.OL", "SBX.OL", "SDRL.OL", "SBO.OL", "SEVDR.OL", "SEVAN.OL", "SIOFF.OL", "SKBN.OL", "SKI.OL", "SKUE.OL", "SOLON.OL", "SOFF.OL", "SOFFB.OL", "SOLV.OL", "SONG.OL", "SBVG.OL", "NONG.OL", "RING.OL", "MING.OL", "SRBANK.OL", "SOAG.OL", "MORG.OL", "SOR.OL", "SVEG.OL", "SPOG.OL", "SPU.OL", "STL.OL", "SNI.OL", "STB.OL", "STORM.OL", "STRONG.OL", "SUBC.OL", "TIL.OL", "TRVX.OL", "TEAM.OL", "TECH.OL", "TEL.OL", "TGS.OL", "SSC.OL", "THIN.OL", "TOM.OL", "TOTG.OL", "TRE.OL", "TTS.OL", "VEI.OL", "VVL.OL", "WWL.OL", "WEIFA.OL", "WRL.OL", "WWI.OL", "WWIB.OL", "WILS.OL", "XXL.OL", "YAR.OL", "ZAL.OL")
dataEnv <- new.env()
out <- sapply(tickers, function(s) tryCatch({ getSymbols(s , env = dataEnv) }, error = function(e) NA))
plist <- eapply(dataEnv, Ad)
pframe <- do.call(merge, plist)
names(pframe) <- gsub(".Adjusted", "", names(pframe))
names(pframe) <- gsub(".X", "", names(pframe))
pframe <- log(pframe)
pframe <- diff(pframe, lag = 1)
pframe<-t(pframe)
library(reshape2)
pframe<- melt(pframe)
library(data.table)
setnames(pframe, old=c("Var1","Var2", "value"), new=c("Ticker", "Date", "Returns"))
# Getting index data
index <- c("^GSPC")
dataEnv1 <- new.env()
out1 <- sapply(index, function(s) tryCatch({ getSymbols(s , env = dataEnv1) }, error = function(e) NA))
index <- eapply(dataEnv1, Ad)
index <- do.call(merge, index)
names(index) <- gsub(".Adjusted", "", names(index))
names(index) <- gsub(".X", "", names(index))
index <- log(index)
index <- diff(index, lag = 1)
index<-t(index)
index<- melt(index)
library(data.table)
setnames(index, old=c("Var1","Var2", "value"), new=c("Index", "Date", "Index_Returns"))
# Getting curr data
curr <- c("NOK=X")
dataEnv1 <- new.env()
out1 <- sapply(curr, function(s) tryCatch({ getSymbols(s , env = dataEnv1) }, error = function(e) NA))
curr <- eapply(dataEnv1, Ad)
curr <- do.call(merge, curr)
names(curr) <- gsub(".Adjusted", "", names(curr))
names(curr) <- gsub(".X", "", names(curr))
curr <- log(curr)
curr <- diff(curr, lag = 1)
curr<-t(curr)
curr<- melt(curr)
library(data.table)
setnames(curr, old=c("Var1","Var2", "value"), new=c("NOK", "Date", "NOK_Returns"))
NOK_index <- merge(index, curr, by="Date")
NOK_index$Index <- NULL
NOK_index$NOK <- NULL
Final <- merge(pframe, NOK_index, by="Date")
Final <- na.omit(Final)
答案 1 :(得分:0)
尝试使用Gson。
Gradle导入:
compile 'com.google.code.gson:gson:2.2.4'
使用gson解析我们的pojo的代码:
JSONObject object = new JSONObject(response);
Gson gson = new Gson();
FeedPojo feedPojo = gson.fromJson(object.toString(), FeedPojo.class);
您的FeedPojo必须实现Serializable:
public class FeedPojo implements Serializable {
...
}