我在解析大型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
是空的。
答案 0 :(得分:0)
您应该尝试将jObject = this.sendData(actionname);
切换为'jObject = new JSONObject(this.sendData(actionname));`
你必须初始化JSON对象而你不是......
答案 1 :(得分: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);
}
}
答案 2 :(得分:0)
这将帮助你Dude ..虽然解析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;