开发者

Android Json Deserialization using Gson fails on inner "data" tag

I am using Gson library in Android to parse JSON.

ResponseList .java

package com.hussain.classes;

import java.util.Date;
import java.util.List;

@SuppressWarnings("unused")
public class ResponseList {

private List<ResponseTag> server_list;

    public List<ResponseTag> getResponse() {
        return server_list;
    }

    public void setTrends(List<ResponseTag> response) {
        this.server_list = response;
    }
}

ResponseTag .java

package com.hussain.classes;

public class ResponseTag {
    private String ServUser;
    private String ServURL;
    private String CustID;

    public String getName() {
        return ServUser;
    }

    public void setName(String ServUser) {
         this.ServUser = ServUser;
    }

    public String getUrl() {
        return ServURL;
    }

    public void setUrl(String ServURL) {
        this.ServURL = ServURL;
    }

    public String getCustID() {
        return CustID;
    }

    public void setCustID(String CustID) {
       this.CustID = CustID;
    }
}

Main.java

package com.hussain.gjson;

import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.net.URI;

import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;

import com.google.gson.Gson;
import com.hussain.classes.ResponseList;
import com.hussain.classes.ResponseTag;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;

public class Main extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        try{
            Log.i("MY INFO", "Json Parser started..");                    
            Gson gson = new Gson();

            // Reader r = new InputStreamReader(getJSONData("http://search.twitter.com/trends.json"));

            Reader r = new InputStreamReader(getJSONData("http://xxxx.com"));

            Log.i("MY INFO", r.toString());
            ResponseList objs = gson.fromJson(r, ResponseList.class);
            Log.i("MY INFO", ""+objs.getResponse().size());

            for(ResponseTag tr : objs.getRespons开发者_JS百科e()){
                Log.i("Response", tr.getName() + " - " + tr.getUrl()+ "-"+ tr.getCustID());         
            }            
        }catch(Exception ex){
            ex.printStackTrace();
        }
    }


    public InputStream getJSONData(String url){
        DefaultHttpClient httpClient = new DefaultHttpClient();
        URI uri;
        InputStream data = null;
        try {
            uri = new URI(url);
            HttpGet method = new HttpGet(uri);
            HttpResponse response = httpClient.execute(method);
            data = response.getEntity().getContent();
        } catch (Exception e) {
            e.printStackTrace();
        }

        return data;
    }
}    

These three java classes are used in my project. It works perfectly and gives back a JSON response As:

{ "server_list" : [ { "CustID" : "23", "ServUser" : "Nathan", "ServPass" : "coffee00", "CustServersID" : "157", "ServURL" : "exp.lcgpence.com" }, ] } }

But when I try the same with a different URL, it fails to work. After focusing on both JSON responses from two different URLs, I find one unique difference as:

{ "sessionId" : "000ASXeEL3381100000000LCG0", "responseCode" : 1, "data" : [ { "job_number" : "09-028", "contract_date" : {}, "project_title" : "Dayton School District Precon", "project_manager" : "Shaun Stuhldryer", "created_date" : "2009-06-11T13:22:08.000000580", "project_name" : "09028" }, { "job_number" : "09-053", "contract_date" : {}, "project_title" : "South Salem High School 2010 Renov.", "project_manager" : "Lee Zumwalt", "created_date" : "2009-11-30T14:38:43.000000573", "project_name" : "09053" }, { "job_number" : "09-011", "contract_date" : {}, "project_title" : "OFP New Concrete Floor - Bay 4", "project_manager" : "Jeff Perrin", "created_date" : "2009-01-19T11:12:56.000000113", "project_name" : "09011" }, { "job_number" : "08-020", "contract_date" : {}, "project_title" : "Clackamas Freezer Repair", "project_manager" : "Paul Schulz", "created_date" : "2008-04-02T11:43:40.000000050", "project_name" : "08020" }, { "job_number" : "07-018", "contract_date" : {}, "project_title" : "Shop Tenant Improvements LL Work", "project_manager" : "Jason Terry", "created_date" : "2007-02-22T17:33:07.000000920", "project_name" : "07018" }}]

The only difference is, that in second JSON response, I have to take "data" and parse inner values. In, first one "server_list" which is the only tag.

How to deserialise inner "data" tag ?


A simple solution would be that you create another class:

public class Whatever {
    private String sessionId;
    private String responseCode;
    private List<ResponseTag> data;
    ...
}

If the deserialization fails the first time, just try it again with the new class.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜