Back to project page ReadabilitySDK.
The source code is released under:
MIT License
If you think the Android project ReadabilitySDK listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
package com.idemidov.readability.parser; // w w w . j a va 2 s.c o m import com.idemidov.readability.data.CommandResponse; import com.idemidov.readability.data.Tag; import com.idemidov.readability.util.ResultCode; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; import java.util.ArrayList; /** * Created by Ilya on 25/02/14. */ public class GetTagsParser implements Parser<ArrayList<Tag>> { @Override public CommandResponse<ArrayList<Tag>> parse(String response) throws JSONException { CommandResponse<ArrayList<Tag>> result = new CommandResponse<ArrayList<Tag>>(); ArrayList<Tag> tags = new ArrayList<Tag>(); JSONObject json = new JSONObject(response); JSONArray jsonTags = json.optJSONArray("tags"); for (int i=0; i < jsonTags.length(); ++i) { tags.add(parseTag(jsonTags.getJSONObject(i))); } result.setCode(ResultCode.CODE_OK); result.setData(tags); return result; } public static Tag parseTag(JSONObject jsonTag) throws JSONException { Tag item = new Tag(); item.setText(jsonTag.optString("text")); item.setId(jsonTag.optInt("id")); item.setAppliedCount(jsonTag.optInt("applied_count")); JSONArray jsonBookmarkIds = jsonTag.optJSONArray("bookmark_ids"); for (int j = 0; j < jsonBookmarkIds.length(); j++) { item.addBookmarkId(jsonBookmarkIds.getInt(j)); } return item; } public static ArrayList<Tag> parseTags(JSONArray jsonTags) throws JSONException { ArrayList<Tag> tags = new ArrayList<Tag>(); for (int i = 0; i < jsonTags.length(); ++i) { Tag tag = new Tag(); tag.setText(jsonTags.getJSONObject(i).optString("text")); tag.setId(jsonTags.getJSONObject(i).optInt("id")); tag.setAppliedCount(jsonTags.getJSONObject(i).optInt("applied_count")); JSONArray jsonBookmarkIds = jsonTags.getJSONObject(i).optJSONArray("bookmark_ids"); for (int j = 0; j < jsonBookmarkIds.length(); j++) { tag.addBookmarkId(jsonBookmarkIds.getInt(j)); } tags.add(tag); } return tags; } }