1

我正在尝试解析以下 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":"
"}]]

这是我的代码:

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

任何人都可以帮忙,我的代码不起作用?我想解析标题、公司名称、类别等...

4

4 回答 4

1

看看我写的使用本机工具和 Gson 库的这个 Json 解析指南:

json解析

也许你会发现它很有用。您也可以从那里下载完整的项目,自己运行和测试它。

于 2013-06-05T11:06:39.897 回答
1

你需要构建你的json。

没有名为“结果”的数组。你要做的就是给 json 的每个元素命名一个唯一的名字,以便获取它。

  {"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":"
    "}]]}
于 2013-06-05T11:07:52.443 回答
1

您需要JSONArray从您的 jsonstring 创建。

你有JSONArray里面JSONArray然后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());
     }
于 2013-06-05T11:15:06.613 回答
1

您可以尝试下面的代码来解析 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"}
]
}

解析代码

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;
}
于 2013-06-05T11:16:32.103 回答