如何在android listview中获取数组内的json数组

时间:2016-11-21 12:17:50

标签: android json listview

我的json格式如下所示,如何将其获取到android中的listview,而我正在尝试这样做,跟随错误上升。我有多个起始数组方括号。

                 org.json.JSONException: Value [{"id":"30","title":"Android Design Engineer","postedDate":"2016-11-19","jobtype":"Contract","location":"Alabama","description":"Basic knowladge in android can give him so many advantages to develop and learna android in an openly sourced android developer in India and hes an outsourcer of the manditory field in and entire world","experience":"2 to 6 yrs","salary":" Upto $50"}] at 0 of type org.json.JSONArray cannot be converted to JSONObject

以下是我的java代码,在这里我调用jsosarray并将其拆分为对象。

        try {
            HttpClient httpClient = new DefaultHttpClient();
            HttpPost httpPost = new HttpPost("http://10.0.3.2/utyessjobsi/jobdetail");
            httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
            HttpResponse httpResponse = httpClient.execute(httpPost);
            int code = httpResponse.getStatusLine().getStatusCode();
            String recode = String.valueOf(code);
            HttpEntity entity = httpResponse.getEntity();
            is = entity.getContent();
            bufferedReader = new BufferedReader(new InputStreamReader(is));
             json_result = bufferedReader.readLine();

            try {
                if (code == 200) {

                    JSONArray jsonArray = new JSONArray(json_result);
                    int length = jsonArray.length();
                    for (int i = 0; i < length; i++) {

                        JSONObject c = jsonArray.getJSONObject(i);

                        String id = c.getString(TAG_ID);
                        String jobname = c.getString(TAG_JOBTITLE);
                        String description = c.getString(TAG_DESC);
                        String jobtype = c.getString(TAG_JOBTYPE);
                        String salary = c.getString(TAG_SALARY);
                        String postedon = c.getString(TAG_POSTEDDATE);
                        String location = c.getString(TAG_LOCATION);
                        String exp = c.getString(TAG_EXPE);


                        HashMap<String, String> result = new HashMap<String, String>();
                        result.put(TAG_ID, id);
                        result.put(TAG_JOBTITLE, jobname);
                        result.put(TAG_DESC, description);
                        result.put(TAG_JOBTYPE, jobtype);
                        result.put(TAG_SALARY, salary);
                        result.put(TAG_POSTEDDATE,postedon);
                        result.put(TAG_LOCATION, location);
                        result.put(TAG_EXPE, exp);


                            resultList.add(result);

                    }
                } else {
                    JSONObject jsonObj = new JSONObject(json_result);
                    status = jsonObj.getString("status");
                    msg = jsonObj.getString("msg");
                }
                return recode;
            } catch (JSONException e) {
                Log.e("Json erroe", e.toString());
                return e.toString();

我的Json数组

 [
  [
   {
      "id": "30",
     "title": "Android Design Engineer",
      "description": "Basic knowladge in android can give him so many  advantages to develop and learna android in an openly sourced android developer in India and hes an outsourcer of the manditory field in and entire world",
  "jobtype": "Contract",
  "salary": " Upto $50",
  "postedDate": "2016-11-19",
  "location": "Alabama",
  "experience": "2 to 6 yrs"
     }
   ],
    [
     {
       "id": "24",
      "title": "Android Application Developer",
        "description": "Android Application Developer is the major Development Technique that is used in this damn World.",
    "jobtype": "Contract",
  "salary": " Upto $50",
  "postedDate": "2016-11-16",
  "location": "North Carolina",
  "experience": "6 to 10 yrs"
      }
   ]
  ]

2 个答案:

答案 0 :(得分:1)

根据您的JSON检查以下解析逻辑:

try {
                HttpClient httpClient = new DefaultHttpClient();
                HttpPost httpPost = new HttpPost("http://10.0.3.2/utyessjobsi/jobdetail");
                httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
                HttpResponse httpResponse = httpClient.execute(httpPost);
                int code = httpResponse.getStatusLine().getStatusCode();
                String recode = String.valueOf(code);
                HttpEntity entity = httpResponse.getEntity();
                is = entity.getContent();
                bufferedReader = new BufferedReader(new InputStreamReader(is));
                json_result = bufferedReader.readLine();

                try {
                    if (code == 200) {
                        JSONArray jsonArray = new JSONArray(json_result);
                        if (jsonArray != null && jsonArray.length() > 0) {
                            for (int i = 0; i < jsonArray.length(); i++) {
                                JSONArray jsonChildArray = jsonArray.getJSONArray(i);
                                if (jsonChildArray != null && jsonChildArray.length() > 0) {
                                    JSONObject c = jsonChildArray.getJSONObject(0);

                                    String id = c.getString(TAG_ID);
                                    String jobname = c.getString(TAG_JOBTITLE);
                                    String description = c.getString(TAG_DESC);
                                    String jobtype = c.getString(TAG_JOBTYPE);
                                    String salary = c.getString(TAG_SALARY);
                                    String postedon = c.getString(TAG_POSTEDDATE);
                                    String location = c.getString(TAG_LOCATION);
                                    String exp = c.getString(TAG_EXPE);


                                    HashMap<String, String> result = new HashMap<String, String>();
                                    result.put(TAG_ID, id);
                                    result.put(TAG_JOBTITLE, jobname);
                                    result.put(TAG_DESC, description);
                                    result.put(TAG_JOBTYPE, jobtype);
                                    result.put(TAG_SALARY, salary);
                                    result.put(TAG_POSTEDDATE, postedon);
                                    result.put(TAG_LOCATION, location);
                                    result.put(TAG_EXPE, exp);


                                    resultList.add(result);
                                }
                            }
                        }
                    } else {
                        JSONObject jsonObj = new JSONObject(json_result);
                        status = jsonObj.getString("status");
                        msg = jsonObj.getString("msg");
                    }
                    return recode;
                } catch (JSONException e) {
                    Log.e("Json erroe", e.toString());
                    return e.toString();
                }
            } catch (Exception e) {
                Log.e("erroe", e.toString());
                return e.toString();
            }

答案 1 :(得分:0)

是。有一个错误。您的JSON数据有一个数组内部数组,您正在尝试将内部数组指定为对象。

首先转换外部数组JsonArray jsonArray1;

遍历此数组。 i = 0 - &gt; jsonArray1.length;并创建另一个

JsonArray jsonArray2 = jsonArray1 [i];

最后遍历jsonArray2:j = 0 - &gt; jsonArray2.length()

并创建一个JsonObject json = jsonArray2 [j];

我希望你理解。这是伪造的。如果你想要代码,请告诉我。我可以写它。