我可以获取部分名称,网址和标题,但我无法从解析json获取最后修改日期此代码为Json
{
"response":{
"status":"ok",
"userTier":"developer",
"total":368,
"startIndex":1,
"pageSize":10,
"currentPage":1,
"pages":37,
"orderBy":"relevance",
"results":[
{
"id":"technology/2017/may/24/fitness-trackers-out-of-step-when-measuring-calories-research-shows",
"type":"article",
"sectionId":"technology",
"sectionName":"Technology",
"webPublicationDate":"2017-05-24T15:00:24Z",
"webTitle":"Fitness trackers out of step when measuring calories, research shows",
"webUrl":"https://www.theguardian.com/technology/2017/may/24/fitness-trackers-out-of-step-when-measuring-calories-research-shows",
"apiUrl":"https://content.guardianapis.com/technology/2017/may/24/fitness-trackers-out-of-step-when-measuring-calories-research-shows",
"fields":{
"headline":"Fitness trackers out of step when measuring calories, research shows",
"lastModified":"2017-05-24T15:02:19Z",
"thumbnail":"https://media.guim.co.uk/8d3e17604195078ec89e20329e2ddc5027eca8ea/0_213_6362_3817/500.jpg"
},
"isHosted":false
},

这是我解析它的代码。
JSONObject response = root.getJSONObject(" response");
if(response.has("results")){
JSONArray results = response.getJSONArray("results");
for(int i=0;i<results.length();i++){
long lastModified=0;
String headline=null;
JSONObject details=results.getJSONObject(i);
String sectionName=details.getString("sectionName");
Log.i(LOG_TAG,sectionName);
String webUrl=details.getString("webUrl");
Log.i(LOG_TAG,webUrl);
if(details.has("fields")){
JSONObject fields=details.getJSONObject("fields");
if(fields.has("headline")){
headline =fields.getString("headline");
Log.i(LOG_TAG,headline);}
if(fields.has("lastModified")){
lastModified =fields.getLong("lastModified");
Log.i(LOG_TAG, String.valueOf(lastModified));}}
我不知道为什么不能获得最后修改日期?
答案 0 :(得分:1)
你可以解析Json并获得像这样的lastModified,
public static void parse(String response) {
try {
JSONObject baseObject = new JSONObject(response);
if (baseObject == null) {
return;
}
JSONObject responseObj = baseObject.optJSONObject("response");
if (response == null) {
return;
}
JSONArray resultsArray = responseObj.getJSONArray("results");
if (resultsArray == null) {
return;
}
for (int i = 0; i < resultsArray.length(); i++) {
JSONObject resultObj = resultsArray.getJSONObject(i);
if (resultObj == null) {
continue;
}
String id = resultObj.optString("id", "");
String type = resultObj.optString("type", "");
String sectionId = resultObj.optString("sectionId", "");
String sectionName = resultObj.optString("sectionName", "");
String webPublicationDate = resultObj.optString("webPublicationDate", "");
String webTitle = resultObj.optString("webTitle", "");
String webUrl = resultObj.optString("webUrl", "");
String apiUrl = resultObj.optString("apiUrl", "");
boolean isHosted = resultObj.optBoolean("isHosted", false);
JSONObject fieldsObj = resultObj.optJSONObject("fields");
if (fieldsObj == null) {
continue;
}
String headline = fieldsObj.optString("headline", "");
String lastModified = fieldsObj.optString("lastModified", "");
String thumbnail = fieldsObj.optString("thumbnail", "");
}
} catch (JSONException e) {
e.printStackTrace();
}
}
将日期转换为特定格式
public static String getDateFromatedString(String inputDate) {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssz");
Date date = null;
try {
date = simpleDateFormat.parse(inputDate);
} catch (ParseException e) {
e.printStackTrace();
}
if (date == null) {
return "";
}
SimpleDateFormat convetDateFormat = new SimpleDateFormat("yyyy-MM-dd");
return convetDateFormat.format(date);
}
答案 1 :(得分:0)
该DateTime格式实际上是ISO 8601 DateTime。 JSON没有为日期/时间指定任何特定格式。如果你有点谷歌,你会发现很多实现用Java来解析它。
如果您愿意使用Java内置的日期/时间/日历类以外的东西,我还建议使用Joda Time。它们(在许多方面)提供了一个ISODateTimeFormat来解析这些类型的字符串。
答案 2 :(得分:0)
您正在使用
lastModified =fields.getLong("lastModified");
你应该使用getString()
,因为你的lastModified日期是String 类型。
答案 3 :(得分:0)
这是我更新的代码,但没有改变
[youtube] mnvShlwsueU: Downloading webpage [youtube] mnvShlwsueU: Downloading video info webpage [youtube] mnvShlwsueU: Extracting video information [youtube] mnvShlwsueU: Downloading MPD manifest
答案 4 :(得分:0)
这个java类
public class News {
private String mTitle;
private String mSectionName;
private String mUrl;
private String mDate;
// constructor of news`
public News(String title,String sectionName,String date,String url){
mTitle =title;
mSectionName=sectionName;
mDate=date;
mUrl=url;}
public News(String title,String sectionName){ mTitle =title;
mSectionName=sectionName;}
// get methods.
public String getmTitle(){return mTitle;}
public String getmSectionName(){return mSectionName;}
public String getmUrl(){return mUrl;}
public String getmDate() {
return mDate;}}