0

首先是:Android代码

public class MachineController extends AsyncTask<String, String,List<Machine>> {

private static String REST_URL = "...";
List<Machine> machines;


@Override
protected List<Machine> doInBackground(String... params) {
    machines = new ArrayList<Machine>();


    HttpClient httpClient = new DefaultHttpClient();
    HttpGet httpGet = new HttpGet(REST_URL);
    httpGet.addHeader("accept", "application/json");

    HttpResponse response;
    try {
        response = httpClient.execute(httpGet);
        HttpEntity entity = response.getEntity();

        if (entity != null) {
            InputStream instream = entity.getContent();

            BufferedReader reader = new BufferedReader(new InputStreamReader(instream));
            StringBuilder sb = new StringBuilder();
            String line = null;

            while ((line = reader.readLine()) != null)
                sb.append(line + "n");

            String result = sb.toString();


            Gson gson = new Gson();
            JsonReader jsonReader = new JsonReader(new StringReader(result));
            jsonReader.setLenient(true);

            JsonParser parser = new JsonParser();
            JsonArray jsonArray = parser.parse(jsonReader).getAsJsonArray();

            for (JsonElement obj : jsonArray) {
                Machine machine = gson.fromJson(obj.getAsJsonObject().get("mobileMachine"), Machine.class);
                machines.add(machine);
                machines.get(0);

            }

            instream.close();

        }
    } catch (ClientProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return machines;
}

}

这是 JSON 文件的一些代码

 [ 
{ "mobileMachine": 
{ "condition":"VERY_GOOD",
 "document":"", . . .
 "mobileCategory": {   "idNr":"1816e5697eb3e0c8442786be5274cb05cff04c06b4338467c8679770bff32313f7f372b5ec2f7527dad0de47d0fb117e",
 "mobileCategoryEng":"Bookletmaker",
 "mobileCategoryGer":"Broschuerenfertigung" }, 
"modelYear":2006, 
Abmessungen: 665x810mm " } }

{ "mobileMachine": 
{
     "condition":"VERY_GOOD"," ...... } } ]

有时里面有一个mobileCategory。mobileCategoryGer 和 mobileCategoryEng 在列表中始终为空。

我无法编辑 JSON 文件!我只想要 Json 文件中 mobileCategoryGer 和 mobileCategoryEng 的值。其余的工作正常。我希望你能理解并帮助我正确解析它。

(对不起我的英语不好)

4

2 回答 2

0

You could check if it has the key with has() method. Also you can get optional value with optJSONObject() and check if it is not null.

JsonArray jsonArray = parser.parse(jsonReader).getAsJsonArray();
try {
    doSomething(jsonArray);
} catch (JSONException e) {  
   Log.wtf("Terrible Failure", e);
}


private void doSomething(JsonArray jsonArray) throws JSONException{
        for (int i=0; i<jsonArray.length(); i++){
            JSONObject obj = jsonArray.getJSONObject(i);
            JSONObject mobileCategory = obj.optJSONObject("mobileCategory");
            if(mobileCategory !=null){
                if(mobileCategory.has("mobileCategoryEng") && mobileCategory.has("mobileCategoryGer") ){
                    String mobileCategoryEng = mobileCategory.getString("mobileCategoryEng");
                    String mobileCategoryGer = mobileCategory.getString("mobileCategoryGer");
                }
            }
        }
    }
于 2013-10-29T15:10:10.167 回答
0

干得好。

    Type listType = new TypeToken<List<Machine>>() {}.getType();
    ArrayList<Result> results = gson.fromJson(result, listType);

这是您修改后的完整代码:

  public class MachineController extends AsyncTask<String, String,List<Machine>> {

     private static String REST_URL = "...";
     List<Machine> machines;


     @Override
   protected List<Machine> doInBackground(String... params) {

    HttpClient httpClient = new DefaultHttpClient();
    HttpGet httpGet = new HttpGet(REST_URL);
    httpGet.addHeader("accept", "application/json");

   HttpResponse response;
   try {
    response = httpClient.execute(httpGet);
    HttpEntity entity = response.getEntity();

    if (entity != null) {
        InputStream instream = entity.getContent();

        BufferedReader reader = new BufferedReader(new InputStreamReader(instream));
        StringBuilder sb = new StringBuilder();
        String line = null;

        while ((line = reader.readLine()) != null)
            sb.append(line + "n");

        String result = sb.toString();


        Gson gson = new Gson();

       Type listType = new TypeToken<List<Machine>>() {}.getType();
       machines= gson.fromJson(result, listType);

        instream.close();

    }
} catch (ClientProtocolException e) {
    e.printStackTrace();
} catch (IOException e) {
    e.printStackTrace();
}
return machines;

}

于 2013-10-29T15:08:32.073 回答