我不知道怎么在android中调用这个json数组...我试过下面的数组类型调用但是没有用, 建议PLZ
["a","b","c","d"]
我的java
JSONArray arrays = null;
JSONObject object = null;
// TODO Auto-generated method stub
super.onPostExecute(result);
String jsonResult=result;
try
{
object = new JSONObject(jsonResult);
}
catch (JSONException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
arrays = new JSONArray(result);
object=arrays.getJSONObject(0);
textView.setText(object[0]+""object[1]));
logcat的
03-06 09:06:40.272: E/AndroidRuntime(5545): FATAL EXCEPTION: main
03-06 09:06:40.272: E/AndroidRuntime(5545): Process: com.example.loading_mysql, PID: 5545
03-06 09:06:40.272: E/AndroidRuntime(5545): java.lang.NullPointerException
03-06 09:06:40.272: E/AndroidRuntime(5545): at com.example.loading_mysql.MainActivity$JSONParser.onPostExecute(MainActivity.java:100)
03-06 09:06:40.272: E/AndroidRuntime(5545): at com.example.loading_mysql.MainActivity$JSONParser.onPostExecute(MainActivity.java:1)
03-06 09:06:40.272: E/AndroidRuntime(5545): at android.os.AsyncTask.finish(AsyncTask.java:632)
03-06 09:06:40.272: E/AndroidRuntime(5545): at android.os.AsyncTask.access$600(AsyncTask.java:177)
03-06 09:06:40.272: E/AndroidRuntime(5545): at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:645)
03-06 09:06:40.272: E/AndroidRuntime(5545): at android.os.Handler.dispatchMessage(Handler.java:102)
03-06 09:06:40.272: E/AndroidRuntime(5545): at android.os.Looper.loop(Looper.java:137)
03-06 09:06:40.272: E/AndroidRuntime(5545): at android.app.ActivityThread.main(ActivityThread.java:4998)
03-06 09:06:40.272: E/AndroidRuntime(5545): at java.lang.reflect.Method.invokeNative(Native Method)
03-06 09:06:40.272: E/AndroidRuntime(5545): at java.lang.reflect.Method.invoke(Method.java:515)
03-06 09:06:40.272: E/AndroidRuntime(5545): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)
03-06 09:06:40.272: E/AndroidRuntime(5545): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593)
03-06 09:06:40.272: E/AndroidRuntime(5545): at dalvik.system.NativeStart.main(Native Method)
我的postexecute ,,,添加代码后你说,,,不起作用stilllkgjkgkgv gdsghsadgsg
protected void onPostExecute(String result) {
String value = null;
JSONArray jr = null;
try {
jr = new JSONArray(result);
} catch (JSONException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
value =jr.getString(0);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
textView.setText(value);
}
答案 0 :(得分:4)
你拥有的是JSONArray
。下面的json中没有JSONObject
。
[
"a",
"b",
"c",
"d"
]
解析
JSONArray jr = new JSONArray(jsonResult);
for(int i=0;i<jr.length();i++)
{
String value = (String) jr.get(i);
}
[
表示json数组节点
{
表示json对象节点
澄清评论中的问题
{
"name": "Froyo",
"version": "Android 2.2"
}
以上是json对象
编辑:
@Override
protected void onPostExecute(String result)
{
super.onPostExecute(result);
String value = null;
try {
JSONArray jr = new JSONArray(result);
value =(String) jr.get(0);
textView.setText(value)
} catch (JSONException e1)
{
e1.printStackTrace();
}
}