简体   繁体   中英

Can't get date modified From Json

i can get section name , url web title and headline but i can't get lastmodified date from parse json this code for 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 }, 

and this my code to parse it.

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));}}

i do not know why not get lastmodified date?

You can parse Json and get the lastModified like this,

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();
    }
}

To convert the date into specific format

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);
}

That DateTime format is actually ISO 8601 DateTime. JSON does not specify any particular format for dates/times. If you Google a bit, you will find plenty of implementations to parse it in Java.

Here's one

If you are open to using something other than Java's built-in Date/Time/Calendar classes, I would also suggest Joda Time. They offer (among many things) a ISODateTimeFormat to parse these kinds of strings.

You are Using lastModified =fields.getLong("lastModified"); whereas you should use getString() because you lastModified date is String Type.

this java class

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;}}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM