Gson - 从Json转换为List<> [英] Gson - Convert from Json to List<>

查看:115
本文介绍了Gson - 从Json转换为List<>的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的json结果如下所示:

我的json结果如下所示:

b
$ b

  {
返回:0,
列表:[
{
Code :524288,
Label:TEST
},
{
Code:524289,
Label:TEST1
},
{
Code:524290,
Label:TEST2
}
]
}

我的代码:

  protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
列表<问题> qstlist = new ArrayList< Questionaire>();
try {
result = new RequestTask()。execute(http:/dd.com/categories/current?owner = ccc)。get();
json = new JSONObject(result);

} catch(InterruptedException e){
// TODO自动生成的catch块
e.printStackTrace();
} catch(ExecutionException e){
// TODO自动生成的catch块
e.printStackTrace();
} catch(JSONException e){
// TODO自动生成的catch块
e.printStackTrace();
}
GsonBuilder gsonb = new GsonBuilder();
Gson gson = gsonb.create();
Type listType = new TypeToken< List< Questionaire>>(){}。getType();
qstlist =(List< Questionaire>)gson.fromJson(result,listType);
问卷qst = null;
qst = gson.fromJson(result,Questionaire.class);

Toast.makeText(MainActivity.this,Result+ qstlist.size(),Toast.LENGTH_SHORT).show();

$ b $ class RequestTask extends AsyncTask< String,String,String> {

@Override
protected String doInBackground(String ... uri){
HttpClient httpclient = new DefaultHttpClient();
HttpResponse响应;
String responseString = null;
尝试{
response = httpclient.execute(new HttpGet(uri [0]));
StatusLine statusLine = response.getStatusLine();
if(statusLine.getStatusCode()== HttpStatus.SC_OK){
ByteArrayOutputStream out = new ByteArrayOutputStream();
response.getEntity()。writeTo(out);
responseString = out.toString();
out.close();
} else {
//关闭连接。
response.getEntity()。getContent()。close();
抛出新的IOException(statusLine.getReasonPhrase());
}
} catch(ClientProtocolException e){
// TODO Handle problems ..
} catch(IOException e){
// TODO Handle problems ..
}
return responseString;
}

@Override
protected void onPostExecute(String result){
super.onPostExecute(result);
}
}

我的调查问卷类:

  public class Questionaire {
String code;
字符串标签;
public String getCode(){
return Code;
}
public void setCode(String Code){
this.Code = Code;
}
public String getLabel(){
return Label;
}
public void setLabel(String Label){
this.Label = Label;


以下是我无法看到的问题在这个JSON中,你得到了一个{Code,Label}对象的列表,但是在 List 一个对象的属性。



首先需要将这个列表封装在另一个对象中。
如:

  public class QuestionaireList {
public List< Questionaire>清单;
}

List< Questionaire> qsts = gson.fromJson(result,QuestionaireList.class).List;


i'm having an issue converting Json to List<>, I have tried different solutions but no clue

My json result looks like :

{  
   "Return":0,
   "List":[  
      {  
         "Code":524288,
         "Label":"TEST"
      },
      {  
         "Code":524289,
         "Label":"TEST1"
      },
      {  
         "Code":524290,
         "Label":"TEST2"
      }
   ]
}

My code :

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    List<Questionaire> qstlist = new ArrayList<Questionaire>();
    try {
        result = new RequestTask().execute("http:/dd.com/categories/current?owner=ccc").get();
        json = new JSONObject(result);

    } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (ExecutionException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (JSONException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    GsonBuilder gsonb = new GsonBuilder();
    Gson gson = gsonb.create();
    Type listType = new TypeToken<List<Questionaire>>(){}.getType();
    qstlist = (List<Questionaire>) gson.fromJson(result, listType);
    Questionaire qst = null;
    qst = gson.fromJson(result,  Questionaire.class);

    Toast.makeText(MainActivity.this, "Result "+qstlist.size(), Toast.LENGTH_SHORT).show(); 
}

class RequestTask extends AsyncTask<String, String, String>{

    @Override
    protected String doInBackground(String... uri) {
        HttpClient httpclient = new DefaultHttpClient();
        HttpResponse response;
        String responseString = null;
        try {
            response = httpclient.execute(new HttpGet(uri[0]));
            StatusLine statusLine = response.getStatusLine();
            if(statusLine.getStatusCode() == HttpStatus.SC_OK){
                ByteArrayOutputStream out = new ByteArrayOutputStream();
                response.getEntity().writeTo(out);
                responseString = out.toString();
                out.close();
            } else{
                //Closes the connection.
                response.getEntity().getContent().close();
                throw new IOException(statusLine.getReasonPhrase());
            }
        } catch (ClientProtocolException e) {
            //TODO Handle problems..
        } catch (IOException e) {
            //TODO Handle problems..
        }
        return responseString;
    }

    @Override
    protected void onPostExecute(String result) {
        super.onPostExecute(result);
    }
}

My questionnaire class :

public class Questionaire {
    String Code;
    String Label;
    public String getCode() {
        return Code;
    }
    public void setCode(String Code) {
        this.Code = Code;
    }
    public String getLabel() {
        return Label;
    }
    public void setLabel(String Label) {
        this.Label = Label;
    }
}

Here is everything I can't see what's wrong about that

解决方案

In this JSON, you get a list of {Code, Label} objects, but in the List property of an object.

You first need to encapsulate this list in an other object. As in:

public class QuestionaireList {
  public List<Questionaire> List;
}

List<Questionaire> qsts = gson.fromJson(result,  QuestionaireList.class).List;

这篇关于Gson - 从Json转换为List&lt;&gt;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆