Json解析器无法在我的应用程序中工作

时间:2014-05-06 13:17:06

标签: android json

我已经调用了一个web服务作为响应,我得到了一个json字符串。我尝试了破坏字符串但我收到错误。 我试过调试,我发现它正在捕获块中。

jsonObject = new JSONObject(result);
            if (jsonObject == null) {
                Toast.makeText(context, "nothing to show", Toast.LENGTH_LONG)
                        .show();
            }
            NewDataSet = jsonObject.getJSONObject("NewDataSet");
            Table = NewDataSet.getJSONObject("Table");
            sync = Table.getJSONArray("Table");
            for (int i = 0; i < sync.length(); i++) {
                JSONObject jobj = sync.getJSONObject(i);
                Event event = new Event();
                String Action_Flag = jobj.getString("Action_Flag");
                event.eventAddress = jobj.getString("Address");
                String Batch_ID = jobj.getString("Batch_ID");
                event.eventCity = jobj.getString("City");
                event.companyCode = jobj.getString("Company_Code");
                event.countryCode = jobj.getString("Country_Code");
                event.eventId = jobj.getString("Event_ID");
                event.eventname = jobj.getString("Event_Name");
                event.venue = jobj.getString("Event_Venue");
                event.iEventCode = jobj.getString("IEvent_Code");
                event.eventStatus = jobj.getString("IEvent_Status_Code");
                String Is_Synced = jobj.getString("Is_Synced");
                String ISponsor_Code = jobj.getString("ISponsor_Code");
                event.startDate = jobj.getString("Setup_Date_Time");
                event.sponsorName = jobj.getString("Sponsor_Name");
                event.state = jobj.getString("State_Code");
                event.endDate = jobj.getString("TearDownDateTime");
                String TearDownDateTime_Val = jobj
                        .getString("TearDownDateTime_Val");

                DatabaseHandler db = new DatabaseHandler(context);
                if (Action_Flag.equals("1")) {
                    long id = db.insertEvents(event);
                    Log.e("Insert value", String.valueOf(id));
                } else if (Action_Flag.equals("2")) {
                    long id = db.updateEvent(event);
                    Log.e("Update value", String.valueOf(id));
                } else {
                    int id = db.deleteEvent(event.eventId);
                    Log.e("delete value", String.valueOf(id));
                }
            }

        } catch (JSONException e) {

            e.printStackTrace();
        }

JSON STRING

{
    "NewDataSet": {
        "Table": [
            {
                "Action_Flag": "1",
                "Address": "Sant Vihar, eastern Highhway.",
                "Batch_ID": "12",
                "City": "Gurgaon",
                "Company_Code": "1",
                "Country_Code": "US",
                "Event_ID": "E041402",
                "Event_Name": "Citi Finance",
                "Event_Venue": "kamani",
                "IEvent_Code": "1960",
                "IEvent_Status_Code": "3",
                "Is_Synced": "0",
                "ISponsor_Code": "12",
                "Setup_Date_Time": "2014-04-04 10:00:00",
                "Sponsor_Name": "CitiFinancial",
                "State_Code": "IN",
                "TearDownDateTime": "2014-04-08 21:00:00",
                "TearDownDateTime_Val": "2014-04-08 10:0"
            },
            {
                "Action_Flag": "1",
                "Address": "Vihar Complex, lal Dongarwadi",
                "Batch_ID": "12",
                "City": "Mumbai",
                "Company_Code": "1",
                "Country_Code": "CAN",
                "Event_ID": "E041406",
                "Event_Name": "City",
                "Event_Venue": "bbb",
                "IEvent_Code": "1964",
                "IEvent_Status_Code": "3",
                "Is_Synced": "0",
                "ISponsor_Code": "12",
                "Setup_Date_Time": "2014-04-09 10:00:00",
                "Sponsor_Name": "CitiFinancial",
                "State_Code": "AB",
                "TearDownDateTime": "2014-04-10 11:00:00",
                "TearDownDateTime_Val": "2014-04-10 -1:0"
            },
            {
                "Action_Flag": "1",
                "Address": "Madhuban Colony,Eastern Highwa",
                "Batch_ID": "12",
                "City": "mumbai",
                "Company_Code": "1",
                "Country_Code": "CAN",
                "Event_ID": "E041407",
                "Event_Name": "Roushh",
                "Event_Venue": "Sultan nagar",
                "IEvent_Code": "1965",
                "IEvent_Status_Code": "3",
                "Is_Synced": "0",
                "ISponsor_Code": "10",
                "Setup_Date_Time": "2014-04-09 00:00:00",
                "Sponsor_Name": "Roush Fenway Racing",
                "State_Code": "AB",
                "TearDownDateTime": "2014-04-10 22:00:00",
                "TearDownDateTime_Val": "2014-04-10 10:0"
            },
            {
                "Action_Flag": "1",
                "Address": "Shewan, Holden Nagar.",
                "Batch_ID": "12",
                "City": "Alwatwa",
                "Company_Code": "1",
                "Country_Code": "CAN",
                "Event_ID": "E041408",
                "Event_Name": "SPC",
                "Event_Venue": "Unit-122",
                "IEvent_Code": "1966",
                "IEvent_Status_Code": "3",
                "Is_Synced": "0",
                "ISponsor_Code": "1",
                "Setup_Date_Time": "2014-04-11 10:00:00",
                "Sponsor_Name": "Aflac",
                "State_Code": "MB",
                "TearDownDateTime": "2014-04-11 20:00:00",
                "TearDownDateTime_Val": "2014-04-11 9:0"
            }
        ]
    }
}

我已经调用了一个web服务作为响应,我得到了一个json字符串。我尝试了破坏字符串但我收到错误。 我试过调试,我发现它正在捕获块中。

1 个答案:

答案 0 :(得分:1)

尝试以下代码: -

在获取表值时使用jsonobject

Table = NewDataSet.getJSONObject("Table");

但它是jsonarray所以如下所示

JSONArray table = NewDataSet.getJSONArray("Table");

注意: -

{}表示jsonobject

[]表示jsonarray