简体   繁体   English

如何解析JSON字符串Android

[英]How to Parse the JSON String Android

I'm trying to Parse the below JSONString 我正在尝试解析下面的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... 我想解析标题,公司名称,类别等...

Take a look at this Json parsing guide using native tools and Gson library that I wrote: 使用我编写的本机工具和Gson库来查看这个Json解析指南:

Json Parsing Json解析

Maybe you will find it useful. 也许你会发现它很有用。 You can download the full project from there as well to run and test it for yourself. 您也可以从那里下载完整的项目,以便自己运行和测试。

You need to structure your json. 你需要构建你的json。

There is no array named "result". 没有名为“result”的数组。 What you have to do is to name every element of the json with a unique name, so as to fetch it. 你要做的是用一个唯一的名称命名json的每个元素,以便获取它。

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":"
    "}]]}

You need to create JSONArray from your jsonstring. 您需要从jsonstring创建JSONArray

You have JSONArray inside JSONArray and then JSONObect .. 您在JSONArrayJSONArray ,然后是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());
     }

You can try below code for parsing JSON 您可以尝试下面的代码来解析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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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