3 回答

TA貢獻(xiàn)1111條經(jīng)驗(yàn) 獲得超0個(gè)贊
一個(gè)瘋狂的主意,嘗試解析它并捕獲異常:
import org.json.*;
public boolean isJSONValid(String test) {
try {
new JSONObject(test);
} catch (JSONException ex) {
// edited, to include @Arthur's comment
// e.g. in case JSONArray is valid as well...
try {
new JSONArray(test);
} catch (JSONException ex1) {
return false;
}
}
return true;
}
這段代碼使用了org.json JSON API實(shí)現(xiàn),該實(shí)現(xiàn)在github,maven和部分Android上可用。

TA貢獻(xiàn)1820條經(jīng)驗(yàn) 獲得超3個(gè)贊
使用Google Gson,您可以使用JsonParser:
import com.google.gson.JsonParser;
JsonParser parser = new JsonParser();
parser.parse(json_string); // throws JsonSyntaxException
添加回答
舉報(bào)