如何使用eclipse读取和反序列化此JSON?

时间:2015-02-27 01:53:53

标签: android asp.net json eclipse serialization

我是Android代码中的新手(使用eclipse),我如何阅读和去除JSON我需要一些例子。

我尝试了这个代码,但只是使用数组和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.access$600(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)

2 个答案:

答案 0 :(得分:0)

因为返回的是jsonobject而不是jsonarray,所以改变

JSONArray jArray = new JSONArray(str);

JSONObject json = new JSONObject(str);

答案 1 :(得分:0)

要将json反序列化为java,您可以使用库GSON

以下是可能对您有所帮助的完整示例

http://java.dzone.com/articles/deserializing-json-java-object