简体   繁体   中英

Issues with JSON Parsing Android

I have Json like this:

{
    "id": 226112,
    "name": "name",
    "min": 1,
    "km": "0.33",
    "url": "5___2_2.htm",
    "departures": [
        {
            "type": "DATA",
            "departures": {
                "5": [
                    "04",
                    "19",
                    "34",
                    "47",
                    "59"
                ],
                "6": [
                    "11",
                    "23",
                    "35",
                    "47",
                    "59"
                ] 
                etc..

And I try to parse it:

private static final String TAG_DEPARTURES = "departures";
private static final String TAG_TYPE = "type";
private static final String TAG_DEPARTURES2 = "departures";
private static  String TAG_HOUR = "5";

...
example

if (jsonStr != null) {
            try {
                JSONObject jsonObj = new JSONObject(jsonStr);

                timetables = jsonObj.getJSONArray(TAG_DEPARTURES);

                for (int i = 0; i < timetables.length(); i++) {
                    JSONObject c = timetables.getJSONObject(i);

                    String type = c.getString(TAG_TYPE);

                    JSONObject departures = c.getJSONObject(TAG_DEPARTURES2);
                    String hour = departures.getString(TAG_HOUR);

                    HashMap<String, String> timetable = new HashMap<String, String>();

                    timetable.put(TAG_TYPE, type);
                    timetable.put(TAG_DEPARTURES2, hour);

                    timetableList.add(timetable);
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
        } else {
            Log.e("ServiceHandler", "Couldn't get any data from the url");
        }
...

Finally I get this:

DATA ["04","19","34","47","59"]

And this is String

["04","19","34","47","59"]

I would like to get String[] tab, where:

tab[0] = "04";
tab[1] = "19";
...

I think your json returned is not like you want.maybe you want an array for the key: 5 , 6 ,but your json showed in question is just a string,so if you can control the json returned by server,please change its format to an string array.

If you can't control the json returned,you should extract the real string by yourself.Like this:

public String[] extractArray(final String str){
    final String strNoBrace = str.substring(1,str.length()-1);
    String[] tempResult = strNoBrace.split(",");
    if(tempResult==null) return null;
    String[] result = new String[tempResult.size()];
    for(int i=0,size=tempResult.size();i<size;++i){
        String temp = tempResult[i];
        result[i] = temp.substring(1,temp.length()-1);
    }
    return result;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM