-1

我在解析大 Json 文件时需要帮助:

{"status":{"code":200,"text":"OK"},"content":[{"proposalId":"12536","providerId":"1","supervisorName":null,"supervisorEmail":"lolbroek_13@hotmail.com","hostInstitution":"qsd","hostCountry":"qsd","hostCity":"QSD","hostTypeId":"1","title":"qsd","status":"available","numberStudents":null,"description":"qsd","categoryId":"34","startDate":null,"endDate":null,"submitDate":null,"expectedWorkload":null,"purpose":null,"workOrganisationId":"1","function":null,"audienceId":"1","preConditions":"","certificationGranted":null,"deliverables":null,"expenses":null,"revenue":null,"vacancies":null,"uploadImg":"http:\/\/193.136.60.238\/\/MUTW2012\/core\/img\/internship.png"},
{"proposalId":"12537","providerId":"1","supervisorName":null,"supervisorEmail":"lolbroek_13@hotmail.com","hostInstitution":"qsd","hostCountry":"qsd","hostCity":"QSD","hostTypeId":"1","title":"qsd","status":"available","numberStudents":null,"description":"qsd","categoryId":"34","startDate":null,"endDate":null,"submitDate":null,"expectedWorkload":null,"purpose":null,"workOrganisationId":"1","function":null,"audienceId":"1","preConditions":"","certificationGranted":null,"deliverables":null,"expenses":null,"revenue":null,"vacancies":null,"uploadImg":"http:\/\/193.136.60.238\/\/MUTW2012\/core\/img\/internship.png"},
{"proposalId":"12538","providerId":"1","supervisorName":null,"supervisorEmail":"lolbroek_13@hotmail.com","hostInstitution":"qsd","hostCountry":"qsd","hostCity":"QSD","hostTypeId":"1","title":"qsd","status":"available","numberStudents":null,"description":"qsd","categoryId":"34","startDate":null,"endDate":null,"submitDate":null,"expectedWorkload":null,"purpose":null,"workOrganisationId":"1","function":null,"audienceId":"1","preConditions":"","certificationGranted":null,"deliverables":null,"expenses":null,"revenue":null,"vacancies":null,"uploadImg":"http:\/\/193.136.60.238\/\/MUTW2012\/core\/img\/internship.png"},
{"proposalId":"12539","providerId":"1","supervisorName":null,"supervisorEmail":"lolbroek_13@hotmail.com","hostInstitution":"qsd","hostCountry":"qsd","hostCity":"QSD","hostTypeId":"1","title":"qsd","status":"available","numberStudents":null,"description":"qsd","categoryId":"34","startDate":null,"endDate":null,"submitDate":null,"expectedWorkload":null,"purpose":null,"workOrganisationId":"1","function":null,"audienceId":"1","preConditions":"","certificationGranted":null,"deliverables":null,"expenses":null,"revenue":null,"vacancies":null,"uploadImg":"http:\/\/193.136.60.238\/\/MUTW2012\/core\/img\/internship.png"},
................[etc]

我需要获取内容中的所有标题和proposalIds。我试着让它像这样:

public ArrayList<String> GetUserProposals(String UserId){
        String actionname = sitename+"accounts/"+UserId+"/proposals";
        try {
           jObject = this.sendData(actionname);//change later
          // return result;
           JSONArray ja = jObject.getJSONArray("content");
           for (int i = 0; i < ja.length(); i++) {
                Log.i("MyDebug", "value----" + ja.getJSONObject(i).getString("title"));
           }
        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        ArrayList<String> returnValue = null;
        return returnValue;

    }

但是JSONArray是空的。

4

3 回答 3

0

你应该尝试切换jObject = this.sendData(actionname);到'jObject = new JSONObject(this.sendData(actionname));`

你必须初始化 JSON 对象,你不是......

于 2012-06-13T08:27:44.597 回答
0

我的 JSON 字符串是

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

我这样解析

        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);
            }
        }
于 2012-06-13T08:37:49.337 回答
0

这将帮助你伙计..在解析 JSON 时会有 json 数组 [] 和 json 对象 {}

   DefaultHttpClient httpclient = new DefaultHttpClient(
            new BasicHttpParams());
    HttpPost httppost = new HttpPost(
            "Your Json URL");

    httppost.setHeader("Content-type", "application/json");

    InputStream inputStream = null;
    String result = null;
    HttpResponse response = null;
    try {
        response = httpclient.execute(httppost);

        HttpEntity entity = response.getEntity();

        inputStream = entity.getContent();
        BufferedReader reader = null;

        reader = new BufferedReader(new InputStreamReader(inputStream,
                "UTF-8"), 8);

        StringBuilder sb = new StringBuilder();

        String line = null;
        try {
            while ((line = reader.readLine()) != null) {
                sb.append(line + "\n");
            }
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        result = sb.toString();
        int k = 0;

        JSONObject jObject = null;

        jObject = new JSONObject(result);

        String aJsonString = jObject.getString("results");
        String name = aJsonString;

        JSONArray jArray = null;

        jArray = jObject.getJSONArray("results");

        JSONObject oneObject = null;
        oneObject = jArray.getJSONObject(0);

        JSONArray jArray1 = null;
        jArray1 = oneObject.getJSONArray("address_components");//keyword

        JSONObject oneObject1 = null;
        oneObject1 = jArray1.getJSONObject(1);

        String oneObjectsItem1 = oneObject1.getString("long_name");//keyword
        City = oneObjectsItem1;

    }

    catch (IOException e) {
        int x = 0;
    }
    return City;
于 2012-06-13T09:02:26.430 回答