-4

我有这个 json 字符串:

{"work_completion_forms": 
     [
         {
             "id": "2a6aae87-af4f-4aa9-8e43-05af2be6d963",  
             "name": "File"
         }, 
         { 
             "id": "b6e35c72-026e-4ef0-8eff-36d040054a93",
             "name": "Comment"
         }
     ]
}

我如何解析它?

我试试这个:

Gson g = new Gson();
Parse p = g.fromJson(result, Parse.class);
String[] s = p.get_work_completion_forms();
for (int i = 0; i < s.length;i++){
    Log.d(s[i], "lol");
}

public class Parse {

    public String[] work_completion_forms;

    public String[] get_work_completion_forms()
    {
       return work_completion_forms;
    }

}
4

1 回答 1

2
String[] work_completion_forms

这是您的代码的问题。它们的类型work_completion_forms不是String,而是更复杂的东西。试试这样

public class WorKCompletionForms {
    private String id;
    private String name;
    // getters and setters
}

WokCompletionForms[] s = p.get_work_completion_forms();
for (int i = 0; i < s.length;i++){
    Log.d("TAG", s[i].getId());
}
于 2013-07-26T05:31:56.070 回答