How to Parse the JSON String Android

2019-01-29 03:20发布

问题:

I'm trying to Parse the below JSONString

[[{"0":"
","title":" Technical Support Analyst in Noida","1":"
","Company Name":" Oracle","2":"
","Category":"Fresher","3":"
","Job Type":"Full Time","4":"
","Location":"Noida","5":"
","Job Qualification":"BE\/BTch\/Bsc\/Others","6":"
","Job Experience":"Freshers","7":"
","Job postdate":"2013-6-05","8":"
"}]]

Here My Code:

// try parse the string to a JSON object
try {
    //jObj = new JSONObject(JsonString);
    JSONArray ja = new JSONArray(result);
    int size = ja.length();
    Log.d("tag", "No of Elements " + ja.length());
} catch (JSONException e) {
    Log.e("JSON Parser", "Error parsing data " + e.toString());
}

Could any one help,My Code is not Working? I want to Parse title,CompanyName,Category Etc...

回答1:

You need to create JSONArray from your jsonstring.

You have JSONArray inside JSONArray and then JSONObect..

 try {
        JSONArray ja = new JSONArray(buffer.toString());
        JSONArray innerJsonArray = ja.getJsonArray(0);
        JSONObject object = innerJsonArray.getJSONObject(0);
        String title = object.getString("title");                
     } 
     catch (JSONException e) {
        Log.e("JSON Parser", "Error parsing data " + e.toString());
     }


回答2:

Take a look at this Json parsing guide using native tools and Gson library that I wrote:

Json Parsing

Maybe you will find it useful. You can download the full project from there as well to run and test it for yourself.



回答3:

You need to structure your json.

There is no array named "result". What you have to do is to name every element of the json with a unique name, so as to fetch it.

such as

  {"result":
    ["result1":["result2":{"0":"
    ","title":" Technical Support Analyst in Noida","1":"
    ","Company Name":" Oracle","2":"
    ","Category":"Fresher","3":"
    ","Job Type":"Full Time","4":"
    ","Location":"Noida","5":"
    ","Job Qualification":"BE\/BTch\/Bsc\/Others","6":"
    ","Job Experience":"Freshers","7":"
    ","Job postdate":"2013-6-05","8":"
    "}]]}


回答4:

You can try below code for parsing JSON

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

parsing code

public static ArrayList<Country> ParseJson(String jsonstring) {

    ArrayList<Country> arrCountries = new ArrayList<Country>();

    String status;
    String message = "";
    try {


        JSONObject json = new JSONObject(jsonstring);

        status = json.getString("result");

        if (status.equalsIgnoreCase("success")) {


            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);
            }
        }

    } catch (JSONException e) {
        Log.e("JSON", "There was an error parsing the JSON", e);
    }
    return arrCountries;
}