我创建了一个autoCompleteTextView,它应该从一个非常复杂的json文件中显示一些值。我不知道如何“深入”到json的目录。我想要的值嵌套在代码中。代码来自http请求。这个论坛中的其他解决方案并没有真正帮助我:( 谢谢:)
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.List;
import org.json.JSONArray;
import org.json.JSONObject;
public class JsonParse {
double current_latitude,current_longitude;
public JsonParse(){}
public JsonParse(double current_latitude,double current_longitude){
this.current_latitude=current_latitude;
this.current_longitude=current_longitude;
}
public List<SuggestGetSet> getParseJsonWCF(String sName)
{
List<SuggestGetSet> ListData = new ArrayList<SuggestGetSet>();
try {
URL js = new URL("something");
URLConnection jc = js.openConnection();
BufferedReader reader = new BufferedReader(new InputStreamReader(jc.getInputStream()));
String line = reader.readLine();
JSONObject jsonResponse = new JSONObject(line);
JSONArray jsonArray = jsonResponse.getJSONArray("something");
for(int i = 0; i < jsonArray.length(); i++){
JSONObject r = jsonArray.getJSONObject(i);
ListData.add(new SuggestGetSet(r.getString("something"),r.getString("something")));
}
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
return ListData;
}
例如像这样的json文件:
{ “ID”:1, “错误”:NULL, “结果”:{ “nPeople”:2, “人”:[{ “namePeople”: “印加”, “功率”: “1235”,” location“:”asdfghjja“,”idPeople“:189,”mainItems“:”brownGem“,”已验证“:false,”description“:”Lorem impsum bla bla“,”linkAvatar“:”avatar_12.jpg“,”经度“:16.2434263,” 纬度 “:89.355118},{” namePeople “:” 玛雅”, “功率”: “1235”, “位置”: “hcjkjhljhl”, “idPeople”:119, “mainItems”: “绿玉”, “已验证”:false,“description”:“Lorem impsum bla bla”,“linkAvatar”:“avatar_6.jpg”,“经度”:16.2434263,“纬度”:89.3551185}]}}
答案 0 :(得分:0)
更深入&#34;在JSON结构中,您只需要从您的JSONObject
获取子元素。
JSONObject jsonObject = (JSONObject) jsonParser.parse(jsonFormattedVehicleResponse);
JSONArray vehicles = (JSONArray) jsonObject.get("vehicle");
for(Object vehicle : vehicles) {
JSONObject vehicleJSON = (JSONObject) vehicle;
if(vehicleJSON.containsKey("subValue")) {
JSONObject subJSON = (JSONObject) vehicleJSON.get("subValue");
//do stuff
}
}
冲洗并重复您必须前往的每个子元素,一直到您正在寻找的信息。尽管如此,这确实需要您熟悉您在请求中获得的JSON。