千呼萬換後...Plurk的官方API終於出來了(http://www.plurk.com/API)
目前支援Java以及Python,其他語言可能要找些地下版的
目前簡單的撰寫了個小bot測試,簡單紀錄一下
由於plurk使用了json此資料格式,所以資料格式其實很早就被推測導致非官方API大量出現
而在這兩天的測試要parse json data時,java有非常多的json的library,簡單的找了星塵
提供的Java Plurk API中使用的json-simple(A simple Java toolkit for JSON) 此library
下載http://code.google.com/p/json-simple/downloads/list
下面是我參考的decode部分
http://code.google.com/p/json-simple/wiki/DecodingExamples
我選擇了實做一個簡單的KeyFinder,source code如下:
import java.io.IOException;
import org.json.simple.parser.ContentHandler;
import org.json.simple.parser.ParseException;
/**
*
* @author Administrator
*/
class KeyFinder implements ContentHandler{
private Object value;
private boolean found = false;
private boolean end = false;
private String key;
private String matchKey;
public void setMatchKey(String matchKey){
this.matchKey = matchKey;
}
public Object getValue(){
return value;
}
public boolean isEnd(){
return end;
}
public void setFound(boolean found){
this.found = found;
}
public boolean isFound(){
return found;
}
public void startJSON() throws ParseException, IOException {
found = false;
end = false;
}
public void endJSON() throws ParseException, IOException {
end = true;
}
public boolean primitive(Object value) throws ParseException, IOException {
if(key != null){
if(key.equals(matchKey)){
found = true;
this.value = value;
key = null;
return false;
}
}
return true;
}
public boolean startArray() throws ParseException, IOException {
return true;
}
public boolean startObject() throws ParseException, IOException {
return true;
}
public boolean startObjectEntry(String key) throws ParseException, IOException {
this.key = key;
return true;
}
public boolean endArray() throws ParseException, IOException {
return false;
}
public boolean endObject() throws ParseException, IOException {
return true;
}
public boolean endObjectEntry() throws ParseException, IOException {
return true;
}
}
搭配上此key finder就可以任意找出自己要的key的對應值了
-------待續----------
沒有留言:
張貼留言