如何使用 Eclipse 读取和反序列化此 JSON?
How can I read and deserialize this JSON with eclipse?
我是 Android 代码(使用 eclipse)的新手,我如何阅读和反序列化这个 JSON 我需要一些例子。
我试过 qith 这段代码,但只适用于数组 php,不适用于 ASPX (.net),当应用程序启动时,就停止了!
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
.detectAll()
.penaltyLog()
.penaltyDialog()
.build());
StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder().detectAll()
.penaltyLog()
.build());
TextView wid = (TextView) findViewById(R.id.wid);
TextView name = (TextView) findViewById(R.id.name);
TextView url = (TextView) findViewById(R.id.url);
JSONObject json = null;
String str = "";
HttpResponse response;
HttpClient myClient = new DefaultHttpClient();
HttpPost myConnection = new HttpPost("https://validafacturas.com/BuzonFacturas/Account/Loginmb.aspx");
try {
response = myClient.execute(myConnection);
str = EntityUtils.toString(response.getEntity(), "UTF-8");
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
try{
JSONArray jArray = new JSONArray(str);
json = jArray.getJSONObject(0);
wid.setText(json.getString("iduser"));
name.setText(json.getString("message"));
url.setText(json.getString("success"));
} catch ( JSONException e) {
e.printStackTrace();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
下一个是我的错误代码:
02-27 01:40:04.562: W/System.err(6820): org.json.JSONException: Value {"iduser":"0","message":"Credenciales de acceso inválidas. Inténtelo de nuevo.","success":"false"} of type org.json.JSONObject cannot be converted to JSONArray
02-27 01:40:04.582: W/System.err(6820): at org.json.JSON.typeMismatch(JSON.java:111)
02-27 01:40:04.582: W/System.err(6820): at org.json.JSONArray.<init>(JSONArray.java:91)
02-27 01:40:04.582: W/System.err(6820): at org.json.JSONArray.<init>(JSONArray.java:103)
02-27 01:40:04.582: W/System.err(6820): at com.tricks.readjsonfromurl.MainActivity.onCreate(MainActivity.java:65)
02-27 01:40:04.594: W/System.err(6820): at android.app.Activity.performCreate(Activity.java:5104)
02-27 01:40:04.594: W/System.err(6820): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
02-27 01:40:04.594: W/System.err(6820): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
02-27 01:40:04.602: W/System.err(6820): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
02-27 01:40:04.602: W/System.err(6820): at android.app.ActivityThread.access0(ActivityThread.java:141)
02-27 01:40:04.602: W/System.err(6820): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
02-27 01:40:04.602: W/System.err(6820): at android.os.Handler.dispatchMessage(Handler.java:99)
02-27 01:40:04.612: W/System.err(6820): at android.os.Looper.loop(Looper.java:137)
02-27 01:40:04.612: W/System.err(6820): at android.app.ActivityThread.main(ActivityThread.java:5041)
02-27 01:40:04.612: W/System.err(6820): at java.lang.reflect.Method.invokeNative(Native Method)
02-27 01:40:04.612: W/System.err(6820): at java.lang.reflect.Method.invoke(Method.java:511)
02-27 01:40:04.622: W/System.err(6820): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
02-27 01:40:04.622: W/System.err(6820): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
02-27 01:40:04.632: W/System.err(6820): at dalvik.system.NativeStart.main(Native Method)
因为 return 是 jsonobject 而不是 jsonarray,所以更改
JSONArray jArray = new JSONArray(str);
到
JSONObject json = new JSONObject(str);
要将 json 反序列化为 java,您可以使用库 GSON
这是完整的示例,可能对您有所帮助
http://java.dzone.com/articles/deserializing-json-java-object
我是 Android 代码(使用 eclipse)的新手,我如何阅读和反序列化这个 JSON 我需要一些例子。
我试过 qith 这段代码,但只适用于数组 php,不适用于 ASPX (.net),当应用程序启动时,就停止了!
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
.detectAll()
.penaltyLog()
.penaltyDialog()
.build());
StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder().detectAll()
.penaltyLog()
.build());
TextView wid = (TextView) findViewById(R.id.wid);
TextView name = (TextView) findViewById(R.id.name);
TextView url = (TextView) findViewById(R.id.url);
JSONObject json = null;
String str = "";
HttpResponse response;
HttpClient myClient = new DefaultHttpClient();
HttpPost myConnection = new HttpPost("https://validafacturas.com/BuzonFacturas/Account/Loginmb.aspx");
try {
response = myClient.execute(myConnection);
str = EntityUtils.toString(response.getEntity(), "UTF-8");
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
try{
JSONArray jArray = new JSONArray(str);
json = jArray.getJSONObject(0);
wid.setText(json.getString("iduser"));
name.setText(json.getString("message"));
url.setText(json.getString("success"));
} catch ( JSONException e) {
e.printStackTrace();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
下一个是我的错误代码:
02-27 01:40:04.562: W/System.err(6820): org.json.JSONException: Value {"iduser":"0","message":"Credenciales de acceso inválidas. Inténtelo de nuevo.","success":"false"} of type org.json.JSONObject cannot be converted to JSONArray
02-27 01:40:04.582: W/System.err(6820): at org.json.JSON.typeMismatch(JSON.java:111)
02-27 01:40:04.582: W/System.err(6820): at org.json.JSONArray.<init>(JSONArray.java:91)
02-27 01:40:04.582: W/System.err(6820): at org.json.JSONArray.<init>(JSONArray.java:103)
02-27 01:40:04.582: W/System.err(6820): at com.tricks.readjsonfromurl.MainActivity.onCreate(MainActivity.java:65)
02-27 01:40:04.594: W/System.err(6820): at android.app.Activity.performCreate(Activity.java:5104)
02-27 01:40:04.594: W/System.err(6820): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
02-27 01:40:04.594: W/System.err(6820): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
02-27 01:40:04.602: W/System.err(6820): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
02-27 01:40:04.602: W/System.err(6820): at android.app.ActivityThread.access0(ActivityThread.java:141)
02-27 01:40:04.602: W/System.err(6820): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
02-27 01:40:04.602: W/System.err(6820): at android.os.Handler.dispatchMessage(Handler.java:99)
02-27 01:40:04.612: W/System.err(6820): at android.os.Looper.loop(Looper.java:137)
02-27 01:40:04.612: W/System.err(6820): at android.app.ActivityThread.main(ActivityThread.java:5041)
02-27 01:40:04.612: W/System.err(6820): at java.lang.reflect.Method.invokeNative(Native Method)
02-27 01:40:04.612: W/System.err(6820): at java.lang.reflect.Method.invoke(Method.java:511)
02-27 01:40:04.622: W/System.err(6820): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
02-27 01:40:04.622: W/System.err(6820): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
02-27 01:40:04.632: W/System.err(6820): at dalvik.system.NativeStart.main(Native Method)
因为 return 是 jsonobject 而不是 jsonarray,所以更改
JSONArray jArray = new JSONArray(str);
到
JSONObject json = new JSONObject(str);
要将 json 反序列化为 java,您可以使用库 GSON
这是完整的示例,可能对您有所帮助
http://java.dzone.com/articles/deserializing-json-java-object