作者热门文章
- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我正在处理的 JSON:
[
{
"score":2.860397,
"show": {
"id": 82,
"url": "http://www.tvmaze.com/shows/82/game-of-thrones",
"name": "Game of Thrones",
"type": "Scripted",
"language": "English",
"genres": [
"Drama", "Adventure", "Fantasy"
],
"status": "Running",
"runtime": 60,
"premiered": "2011-04-17",
"schedule": {
"time": "21:00",
"days": [ "Sunday" ]
},
"rating": {
"average": 9.4
},
"weight": 100,
"network": {
"id": 8,
"name": "HBO",
"country": {
"name": "United States",
"code": "US",
"timezone": "America/New_York"
}
},
"webChannel": null,
"externals": {
"tvrage": 24493,
"thetvdb": 121361,
"imdb": "tt0944947"
},
"image": {
"medium": "http://tvmazecdn.com/uploads/images/medium_portrait/53/132622.jpg",
"original": "http://tvmazecdn.com/uploads/images/original_untouched/53/132622.jpg"
},
"summary": "<p>Based on the bestselling book series A Song of Ice and Fire by George R.R. Martin, this sprawling new HBO drama is set in a world where summers span decades and winters can last a lifetime. From the scheming south and the savage eastern lands, to the frozen north and ancient Wall that protects the realm from the mysterious darkness beyond, the powerful families of the Seven Kingdoms are locked in a battle for the Iron Throne. This is a story of duplicity and treachery, nobility and honor, conquest and triumph. In the <strong><em>\"Game of Thrones\"</em></strong>, you either win or you die.</p>",
"updated": 1462561599,
"_links": {
"self": {
"href": "http://api.tvmaze.com/shows/82"
},
"previousepisode": {
"href": "http://api.tvmaze.com/episodes/664672"
},
"nextepisode": {
"href": "http://api.tvmaze.com/episodes/664673"
}
}
}
}
]
我的代码:
JsonArrayRequest request = new JsonArrayRequest(Request.Method.GET, final_url, (String)null,
new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
try {
JSONArray jArray = new JSONArray(response);
for(int i=0; i<jArray.length(); i++) {
JSONObject json_object = (JSONObject)jArray.get(i);
String name = json_object.getString("name");
System.out.println(name);
}
} catch(JSONException e) {
System.out.println(e);
}
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
System.out.println(error);
}
}
);
我看到有很多这样的问题,但其中大部分涉及 JSONObject
而不是 JSONArray
,而且我没有找到适合我的解决方案。
我仍然收到标题中提到的错误。有帮助吗?
更新:到目前为止尝试了除 gson 之外的所有内容,但仍然无法正常工作。堆栈跟踪是:
05-08 14:18:14.790 2628-2628/com.example.user.test W/System.err: org.json.JSONException: Not a primitive array: class org.json.JSONArray
05-08 14:18:14.790 2628-2628/com.example.user.test W/System.err: at org.json.JSONArray.<init>(JSONArray.java:116)
05-08 14:18:14.790 2628-2628/com.example.user.test W/System.err: at com.example.user.test.SearchActivity$2$override.onResponse(SearchActivity.java:134)
05-08 14:18:14.790 2628-2628/com.example.user.test W/System.err: at com.example.user.test.SearchActivity$2$override.access$dispatch(SearchActivity.java)
05-08 14:18:14.790 2628-2628/com.example.user.test W/System.err: at com.example.user.test.SearchActivity$2.onResponse(SearchActivity.java:0)
05-08 14:18:14.790 2628-2628/com.example.user.test W/System.err: at com.example.user.test.SearchActivity$2.onResponse(SearchActivity.java:115)
05-08 14:18:14.790 2628-2628/com.example.user.test W/System.err: at com.android.volley.toolbox.JsonRequest.deliverResponse(JsonRequest.java:72)
05-08 14:18:14.790 2628-2628/com.example.user.test W/System.err: at com.android.volley.ExecutorDelivery$ResponseDeliveryRunnable.run(ExecutorDelivery.java:99)
05-08 14:18:14.790 2628-2628/com.example.user.test W/System.err: at android.os.Handler.handleCallback(Handler.java:739)
05-08 14:18:14.790 2628-2628/com.example.user.test W/System.err: at android.os.Handler.dispatchMessage(Handler.java:95)
05-08 14:18:14.790 2628-2628/com.example.user.test W/System.err: at android.os.Looper.loop(Looper.java:135)
05-08 14:18:14.790 2628-2628/com.example.user.test W/System.err: at android.app.ActivityThread.main(ActivityThread.java:5254)
05-08 14:18:14.797 2628-2628/com.example.user.test W/System.err: at java.lang.reflect.Method.invoke(Native Method)
05-08 14:18:14.797 2628-2628/com.example.user.test W/System.err: at java.lang.reflect.Method.invoke(Method.java:372)
05-08 14:18:14.801 2628-2628/com.example.user.test W/System.err: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
05-08 14:18:14.801 2628-2628/com.example.user.test W/System.err: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
更新 2:我的 wole searchActivity.java 也许错误在其他地方(我删除了数组以查看它是否改变了任何东西但没有):
公共(public)类 SearchActivity 扩展 AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_search);
final EditText editText = (EditText) findViewById(R.id.searchView);
editText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
boolean handled = false;
if (actionId == EditorInfo.IME_ACTION_SEARCH) {
String query = editText.getText().toString();
//doMySearch(query);
clearQuery(query);
handled = true;
}
return handled;
}
});
}
public void clearQuery(String query) {
query = query.replaceAll("[^\\w\\s]","").replaceAll("\\s+","-"); //TVMaze API uses "-" as spaces
doMySearch(query);
}
public void doMySearch(String query) {
System.out.println("print if doMySearch was opened");
System.out.println(query);
List<Show> subscribedShowsList = new ArrayList<Show>();
String url = "http://api.tvmaze.com/search/shows?q=";
String final_url = url + query;
RequestQueue mRequestQueue;
// Instantiate the cache
Cache cache = new DiskBasedCache(getCacheDir(), 1024 * 1024); // 1MB cap
// Set up the network to use HttpURLConnection as the HTTP client.
Network network = new BasicNetwork(new HurlStack());
// Instantiate the RequestQueue with the cache and network.
mRequestQueue = new RequestQueue(cache, network);
JsonArrayRequest request = new JsonArrayRequest(Request.Method.GET, final_url, (String)null,
new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
System.out.println("print if we got to onResponse");
try {
JSONArray jArray = new JSONArray(response);
JSONObject joParent = response.getJSONObject(0);
JSONObject joShow = joParent.getJSONObject("show");
String name = joShow.getString("name");
System.out.println(name);
} catch(JSONException e) {
e.printStackTrace();
}
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
System.out.println(error);
}
});
mRequestQueue.add(request);
mRequestQueue.start();
}
最佳答案
希望这对您有所帮助。
try {
JSONObject joParent = response.getJSONObject(0);
JSONObject joShow = joParent.getJSONObject("show");
String name = joShow.getString("name");
} catch (JSONException e) {
e.printStackTrace();
}
关于android - JSON异常 : Not a primitive array in parsing JSONArray,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37099436/
我是一名优秀的程序员,十分优秀!