簡體   English   中英

如何解析JSON字符串Android

[英]How to Parse the JSON String Android

我正在嘗試解析下面的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());
}

任何人都可以幫忙,我的守則不起作用? 我想解析標題,公司名稱,類別等...

使用我編寫的本機工具和Gson庫來查看這個Json解析指南:

Json解析

也許你會發現它很有用。 您也可以從那里下載完整的項目,以便自己運行和測試。

你需要構建你的json。

沒有名為“result”的數組。 你要做的是用一個唯一的名稱命名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":"
    "}]]}

您需要從jsonstring創建JSONArray

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

您可以嘗試下面的代碼來解析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;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM