0

I am scrapping data from website. The data we are getting is in the format of

[
[
    {
        "train": "South Coast train (CityRail) ",
        "depTime": "7:26pm",
        "depStation": "Albion Park Station ",
        "depPlatform": "1",
        "arrTime": "7:46pm",
        "arrStation": "Wollongong Station ",
        "arrPlatform": "1"
    },
    {
        "train": "South Coast train (CityRail) ",
        "depTime": "8:00pm",
        "depStation": "Wollongong Station ",
        "depPlatform": "1",
        "arrTime": "9:14pm",
        "arrStation": "Hurstville Station ",
        "arrPlatform": "3"
    },
    {
        "train": "Eastern Suburbs and Illawarra train (CityRail) ",
        "depTime": "9:20pm",
        "depStation": "Hurstville Station ",
        "depPlatform": "3",
        "arrTime": "9:24pm",
        "arrStation": "Kogarah Station ",
        "arrPlatform": "3"
    },
    {
        "train": "Eastern Suburbs and Illawarra train (CityRail) ",
        "depTime": "9:27pm",
        "depStation": "Kogarah Station ",
        "depPlatform": "4",
        "arrTime": "9:31pm",
        "arrStation": "Allawah Station ",
        "arrPlatform": "4"
    }
],
[
    {
        "train": "South Coast train (CityRail) ",
        "depTime": "7:26pm",
        "depStation": "Albion Park Station ",
        "depPlatform": "1",
        "arrTime": "7:46pm",
        "arrStation": "Wollongong Station ",
        "arrPlatform": "1"
    },
    {
        "train": "South Coast train (CityRail) ",
        "depTime": "8:00pm",
        "depStation": "Wollongong Station ",
        "depPlatform": "1",
        "arrTime": "9:14pm",
        "arrStation": "Hurstville Station ",
        "arrPlatform": "3"
    },
    {
        "train": "Eastern Suburbs and Illawarra train (CityRail) ",
        "depTime": "9:30pm",
        "depStation": "Hurstville Station ",
        "depPlatform": "3",
        "arrTime": "9:32pm",
        "arrStation": "Allawah Station ",
        "arrPlatform": "3"
    }
],
[
    {
        "train": "South Coast train (CityRail) ",
        "depTime": "8:16pm",
        "depStation": "Albion Park Station ",
        "depPlatform": "1",
        "arrTime": "8:38pm",
        "arrStation": "Wollongong Station ",
        "arrPlatform": "1"
    },
    {
        "train": "South Coast train (CityRail) ",
        "depTime": "8:54pm",
        "depStation": "Wollongong Station ",
        "depPlatform": "1",
        "arrTime": "10:14pm",
        "arrStation": "Hurstville Station ",
        "arrPlatform": "3"
    },
    {
        "train": "Eastern Suburbs and Illawarra train (CityRail) ",
        "depTime": "10:30pm",
        "depStation": "Hurstville Station ",
        "depPlatform": "3",
        "arrTime": "10:32pm",
        "arrStation": "Allawah Station ",
        "arrPlatform": "3"
    }
],
[
    {
        "train": "South Coast train (CityRail) ",
        "depTime": "9:23pm",
        "depStation": "Albion Park Station ",
        "depPlatform": "1",
        "arrTime": "9:45pm",
        "arrStation": "Wollongong Station ",
        "arrPlatform": "1"
    },
    {
        "train": "South Coast train (CityRail) ",
        "depTime": "9:54pm",
        "depStation": "Wollongong Station ",
        "depPlatform": "1",
        "arrTime": "11:14pm",
        "arrStation": "Hurstville Station ",
        "arrPlatform": "3"
    },
    {
        "train": "Eastern Suburbs and Illawarra train (CityRail) ",
        "depTime": "11:30pm",
        "depStation": "Hurstville Station ",
        "depPlatform": "3",
        "arrTime": "11:32pm",
        "arrStation": "Allawah Station ",
        "arrPlatform": "3"
    }
],
{
    "1": {
        "train": "Eastern Suburbs and Illawarra train (CityRail) ",
        "depTime": "5:09am+",
        "depStation": "Hurstville Station ",
        "depPlatform": "3",
        "arrTime": "5:11am+",
        "arrStation": "Allawah Station ",
        "arrPlatform": "3"
    }
}

]

when i am converting above data to Json format i am getting the following error

    06-11 18:36:40.584: V/Error(657): Error in parsing Innerorg.json.JSONException: Value {"1":{"depTime":"5:09am+","arrStation":"Allawah Station ","arrTime":"5:11am+","train":"Eastern Suburbs and Illawarra train (CityRail) ","arrPlatform":"3","depStation":"Hurstville Station ","depPlatform":"3"}} at 4 of type org.json.JSONObject cannot be converted to JSONArray

How to parse this type of json strings in Android. Any advice friends........?

4
  • 1
    your above json string is not valid paste here and see Commented Jun 11, 2012 at 13:12
  • Your json format is wrong.You can validate it on jsonlint.com Commented Jun 11, 2012 at 13:12
  • json format is not valid, you can check json format from jsonlint.com Commented Jun 11, 2012 at 13:15
  • i have just entered format of the part of my json.Now i have updated the complete json string. Commented Jun 11, 2012 at 13:26

2 Answers 2

1

That's because your JSON string is not well constructed.

Try to put it into this: http://jsonlint.com/ it will tell your current errors.

Also please read some documentations about JSON here http://json.org/ because your are actually making some basics error :)

If you also provide what your JSON has to rappresent I can show the correct form of your JSON string :)

Sign up to request clarification or add additional context in comments.

1 Comment

Right. This Json contains 2 root elements, which is not valid.
0

My string was like below

{
"result": "success",
"countryCodeList": [
{
"countryName": "World Wide",
"countryCode": "00"
},
{
"countryName": "Korea, Republic of",
"countryCode": "kr"
},
{
"countryName": "United States",
"countryCode": "us"
},-
{
"countryName": "Japan",
"countryCode": "jp"
},
{
"countryName": "China",
"countryCode": "cn"
},
{
"countryName": "India",
"countryCode": "in"
}
]
}

i have done

JSONObject json = new JSONObject(jsonstring);
JSONArray nameArray = json.names();
JSONArray valArray = json.toJSONArray(nameArray);

JSONArray valArray1 = valArray.getJSONArray(1);

    valArray1.toString().replace("[", "");
            valArray1.toString().replace("]", "");

            int len = valArray1.length();

            for (int i = 0; i < valArray1.length(); i++) {

                Country country = new Country();
                JSONObject arr = valArray1.getJSONObject(i);

                country.setCountryCode(arr.getString("countryCode"));
                country.setCountryName(arr.getString("countryName"));
                arrCountries.add(country);
            }

1 Comment

I just entered part of the my string. actually its a valid json format. I have checked in jsonlint.org. If you want you can check my string now

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.