[英]How to convert JSONTokener to JSONArray and JSONObject in android?
I want to cast a JSONTokener to JSONArray and JSONObject, I know that is very simple and i did some search on but sample codes doesn't work for me,what is wrong? 我想将JSONTokener转换为JSONArray和JSONObject,我知道这很简单,我进行了一些搜索,但示例代码对我不起作用,这是怎么回事?
StringBuilder builder= new StringBuilder();
//some codes to fill my StringBuilder
JSONTokener tokener = new JSONTokener( builder.toString() );
JSONArray finalResult = new JSONArray( tokener );
//error: Unhandle exeception:org.json.JsonExeception
or 要么
JSONObject myJSONObject=new JSONObject(tokener)
//error: Unhandle exeception:org.json.JsonExeception
What is wrong with this codes ? 此代码有什么问题?
Don't try to create JSONObject from tokener. 不要尝试从令牌生成器创建JSONObject。 Simply cast its
.nextValue()
. 只需转换其
.nextValue()
。
From the official example: 从官方示例:
JSONObject object = (JSONObject) new JSONTokener(json).nextValue();
String query = object.getString("query");
JSONArray locations = object.getJSONArray("locations");
https://developer.android.com/reference/org/json/JSONTokener.html https://developer.android.com/reference/org/json/JSONTokener.html
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.