gpt4 book ai didi

javascript - 无法从 Json 获取修改日期

转载 作者:行者123 更新时间:2023-12-02 12:56:20 30 4
gpt4 key购买 nike

我可以获取部分名称、url 网页标题和标题,但我无法从解析 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 响应 = 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));}}

我不知道为什么得不到最后修改日期?

最佳答案

你可以像这样解析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);
}

关于javascript - 无法从 Json 获取修改日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44418108/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com