如何使用改进解析JSON响应

时间:2016-04-29 06:47:20

标签: android json retrofit

我必须解析两件事JSON数组和JSON元素。如何使用retrofit解析JSON元素。

JSON响应示例:

{
  "trip_id": "trip101",
  "itinerary": [
    {
      "day": 1,
      "name": "Arrive Srinagar 5200ft"
    },
    {
      "day": 2,
      "name": "Proceed for Gulmarg 8530ft. (App. 60 Kms / 2 Hrs)"
    },
    {
      "day": 3,
      "name": "Proceed to pahalgam on 5700ft. (App. 140 Kms / 5 Hrs)"
    },
    {
      "day": 4,
      "name": "Free for local Sight seens"
    },
    {
      "day": 5,
      "name": "Proceed for Srinagar (App. 97 Kms / 3 Hrs)"
    },
    {
      "day": 6,
      "name": "Proceed for Day trip to Sonamarg 9200ft.(App. 87 Kms / 3 Hrs)"
    },
    {
      "day": 7,
      "name": "Srinagar Air port Droping"
    }
  ]
}

这是我的pojo课程:

    public class MyPojo{

    private String trip_id;

    private Itinerary[] itinerary;

    public String getTrip_id ()
    {
        return trip_id;
    }

    public void setTrip_id (String trip_id)
    {
        this.trip_id = trip_id;
    }

    public Itinerary[] getItinerary ()
    {
        return itinerary;
    }

    public void setItinerary (Itinerary[] itinerary)
    {
        this.itinerary = itinerary;
    }

    @Override
    public String toString()
    {
        return "ClassPojo [trip_id = "+trip_id+", itinerary = "+itinerary+"]";
    }
}

另一堂课:

public class Itinerary{
    private String name;

    private String day;

    public String getName () {
        return name;
    }

    public void setName (String name) {

        this.name = name;
    }

    public String getDay () {
        return day;
    }

    public void setDay (String day) {

        this.day = day;
    }

    @Override
    public String toString()
    {
        return "ClassPojo [name = "+name+", day = "+day+"]";
    }
}

适配器:

    public class DayAdapter extends ArrayAdapter<Itinerary> {

        private Context context;
        private List<Itinerary> days;


        public DayAdapter(Context context, int resource, List<Itinerary> objects) {
            super(context, resource, objects);
            this.context = context;
            this.days = objects;
        }
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
        View view = inflater.inflate(R.layout.item_day, parent, false);

        Itinerary itinerary = days.get(position);

        TextView tvDay = (TextView)view.findViewById(R.id.tv_day);
        TextView tvName = (TextView)view.findViewById(R.id.tv_name);

        tvDay.setText(itinerary.getDay());
        tvName.setText(itinerary.getName());

        return view;
    }
}

MainActivity:

public class MainActivity extends AppCompatActivity {

    public static final String ROOT_URL = "http://demo8000805.mockable.io/v1/";
    private ListView lvDays;
    List<Itinerary> days;
    String id;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        lvDays = (ListView) findViewById(R.id.lv_days);


        //final ProgressDialog loading = ProgressDialog.show(this, "Fetching Data", "Please wait...", false, false);

        RestAdapter adapter = new RestAdapter.Builder().setEndpoint(ROOT_URL).build();

        DayAPI api = adapter.create(DayAPI.class);

        api.getDays(new Callback<List<Itinerary>>() {
            @Override
            public void success(List<Itinerary> itineraries, Response response) {
                //loading.dismiss();
                days = itineraries;
                DayAdapter dayadapter = new DayAdapter(getApplicationContext(),R.layout.item_day,days);
                lvDays.setAdapter(dayadapter);
            }

            @Override
            public void failure(RetrofitError error) {
                //tvName.setText(error.getMessage());
                Toast.makeText(getApplicationContext(),"Failed", Toast.LENGTH_LONG).show();
            }
        });
    }

如何使用此代码解析JSON响应?

1 个答案:

答案 0 :(得分:0)

您的回调成功结果不是一个行程数组,它是一个包含行程列表的对象。

你的回调必须是这样的:

api.getDays(new Callback<ItineraryResponse>() {
    @Override
    public void success(ItineraryResponse itineraryResponse, Response response) {
        for(Itinerary itinerary : itineraryResponse.getItinerary()){
           Log.e("Name", itinerary.getName());
        }
    }

    @Override
    public void failure(RetrofitError error) {
        //tvName.setText(error.getMessage());
        Toast.makeText(getApplicationContext(),"Failed", Toast.LENGTH_LONG).show();
    }
});

然后你的模型ItineraryResponse必须实现Serializable:

public class ItineraryResponse implements Serializable{

   private ArrayList<Itinerary> itinerary;

   public ArrayList<Itinerary> getItinerary() {
       return itinerary;
   }

   public void setItinerary(ArrayList<Itinerary> itinerary) {
       this.itinerary = itinerary;
   }
}

最后你的模型行程:

public class Itinerary implements Serializable{

   private String name;

   public String getName() {
       return name;
   }

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