Json解析器不工作

时间:2014-05-02 08:14:36

标签: android json

我有这个json值,但我无法解析它。每当我执行应用程序时,它都会强行关闭。 我解析错了吗?

JSON

{
    "NewDataSet": {
        "Table": {
            "Company_Code": "1",
            "Company_Name": "Roush",
            "First_Name": "Driver",
            "ISponsor_Code": "0",
            "Last_Name": "Test003",
            "Password": "cObH0Gh7caq9OhgCN0C3fA==",
            "Sponsor_ID": null,
            "Sponsor_Name": null,
            "UploadEvents": "false",
            "User_Id": "driver003",
            "User_Name": "Driver Test003",
            "User_Type": "Driver"
        }
    }
}

MAIN CLASS

protected void onPostExecute(String s) {
                super.onPostExecute (s);
                // Log.d ("hello", s);


                try {
                    jsonObject = new JSONObject (s);
                    NewDataSet = jsonObject.getJSONObject ("NewDataSet");
                    Log.d("",NewDataSet.toString ());

                    Table = jsonObject.getJSONObject ("Table");
                    String Company_Code = Table.getString ("Company_Code");
                    String Company_Name = Table.getString ("Company_Name");
                    String First_Name = Table.getString ("First_Name");
                    String ISponsor_Code = Table.getString ("ISponsor_Code");
                    String Last_Name = Table.getString ("Last_Name");
                    String Password = Table.getString ("Password");
                    String Sponsor_ID = Table.getString ("Sponsor_ID");
                    String Sponsor_Name = Table.getString ("Sponsor_Name");
                    String UploadEvents = Table.getString ("UploadEvents");
                    String User_Id = Table.getString ("User_Id");
                    String User_Name = Table.getString ("User_Name");
                    String User_Type = Table.getString ("User_Type");
                    tv.setText (Company_Code + ":" + Company_Name + ":" + First_Name + ":" + ISponsor_Code + ":" + Last_Name + ":" + Password + ":" + Sponsor_ID + ":" + Sponsor_Name + ":" + UploadEvents + ":" + User_Id + ":" + User_Name + ":" + User_Type);

                } catch (JSONException e) {
                    e.printStackTrace ();
                }


            }
        }
    }

2 个答案:

答案 0 :(得分:4)

你错了就在这里

NewDataSet = jsonObject.getJSONObject ("NewDataSet");
Log.d("",NewDataSet.toString ());
Table = jsonObject.getJSONObject ("Table"); // Table can be access via NewDataSet 

这应该是

NewDataSet = jsonObject.getJSONObject ("NewDataSet");
Log.d("",NewDataSet.toString ());
Table = NewDataSet.getJSONObject ("Table");

大多数 : -

您需要阅读Java编程风格。方法名称必须以小写字母而非Caps开头。阅读Java Programming Style Guide

答案 1 :(得分:1)

这个可以帮助你:

protected void onPostExecute(String s) {
            super.onPostExecute (s);
            // Log.d ("hello", s);


            try {
                jsonObject = new JSONObject (s);
                NewDataSet = jsonObject.getJSONObject ("NewDataSet");
                Log.d("",NewDataSet.toString ());

                JSONObject tableObj= NewDataSet.getJSONObject ("Table");
                String Company_Code = tableObj.getString ("Company_Code");
                String Company_Name = tableObj.getString ("Company_Name");
                String First_Name = tableObj.getString ("First_Name");
                String ISponsor_Code = tableObj.getString ("ISponsor_Code");
                String Last_Name = tableObj.getString ("Last_Name");
                String Password = tableObj.getString ("Password");
                String Sponsor_ID = tableObj.getString ("Sponsor_ID");
                String Sponsor_Name = tableObj.getString ("Sponsor_Name");
                String UploadEvents = tableObj.getString ("UploadEvents");
                String User_Id = tableObj.getString ("User_Id");
                String User_Name = tableObj.getString ("User_Name");
                String User_Type = tableObj.getString ("User_Type");
                tv.setText (Company_Code + ":" + Company_Name + ":" + First_Name + ":" + ISponsor_Code + ":" + Last_Name + ":" + Password + ":" + Sponsor_ID + ":" + Sponsor_Name + ":" + UploadEvents + ":" + User_Id + ":" + User_Name + ":" + User_Type);

            } catch (JSONException e) {
                e.printStackTrace ();
            }


        }
    }
}