2017-03-09 504 views
0

我花了兩天時間思考這個問題,但未能解決。 所以請幫助。如何將JSONArray轉換爲ArrayList?

這裏是我的JSON模式

{ 
    "order_room_number": "string", 
    "order_breakfast": "string", 
    "room_type_name": "string", 
    "order_number": "string", 
    "hotel_id": 0, 
    "order_check_in_time": "2017-03-09T10:56:09.343Z", 
    "order_price": "string", 
    "hotel_addres": "string", 
    "order_check_out_time": "2017-03-09T10:56:09.343Z", 
    "hotel_name": "string", 
    "order_phone": "string", 
    "order_transactionid": "string", 
    "order_date": "2017-03-09T10:56:09.343Z", 
    "user_id": 0, 
    "order_people_number": 0, 
    "order_id": 0, 
    "order_state": "string", 
    "order_number_of_room": 0, 
    "orderUserList": [ 
    { 
     "order_user_id": 0, 
     "order_user_name": "string", 
     "order_user_phone": "string", 
     "order_id": 0, 
     "order_user_id_number": "string" 
    } 
    ] 
} 

這裏是我的JSON,我需要將其轉換爲ArrayList的

{ 
    "status": 200, 
    "msg": "OK", 
    "data": [ 
    { 
     "order_id": 12, 
     "order_number": null, 
     "order_transactionid": "9", 
     "order_date": null, 
     "order_check_in_time": 1488680530000, 
     "order_check_out_time": 1488680530000, 
     "order_breakfast": null, 
     "order_number_of_room": null, 
     "order_room_number": null, 
     "order_people_number": null, 
     "order_phone": null, 
     "order_price": "9", 
     "order_state": "1", 
     "room_type_name": null, 
     "hotel_name": null, 
     "hotel_addres": null, 
     "hotel_id": null, 
     "user_id": null, 
     "orderUserList": null 
    } 
    ] 
} 

這裏是我的模型

package com.example.wecheatpaydemo.Model; 

/** 
* 
* 
* @Explain: 
*/ 

public class OrderUser { 


    private Long order_user_id;    // 用戶實名制裏面的姓名 
    private String order_user_name;   // 用戶實名制裏面的姓名 
    private String order_user_phone;  // 用戶的手機號,用作登錄賬號 
    private String order_user_id_number; // 用戶身份證號碼 
    private String order_id;    // 訂單表外鍵 

    public Long getOrder_user_id() { 
     return order_user_id; 
    } 

    public void setOrder_user_id(Long order_user_id) { 
     this.order_user_id = order_user_id; 
    } 

    public String getOrder_user_name() { 
     return order_user_name; 
    } 

    public void setOrder_user_name(String order_user_name) { 
     this.order_user_name = order_user_name; 
    } 

    public String getOrder_user_phone() { 
     return order_user_phone; 
    } 

    public void setOrder_user_phone(String order_user_phone) { 
     this.order_user_phone = order_user_phone; 
    } 

    public String getOrder_user_id_number() { 
     return order_user_id_number; 
    } 

    public void setOrder_user_id_number(String order_user_id_number) { 
     this.order_user_id_number = order_user_id_number; 
    } 

    public String getOrder_id() { 
     return order_id; 
    } 

    public void setOrder_id(String order_id) { 
     this.order_id = order_id; 
    } 

    @Override 
    public String toString() { 
     return "OrderUser{" + 
       "order_user_id=" + order_user_id + 
       ", order_user_name='" + order_user_name + '\'' + 
       ", order_user_phone='" + order_user_phone + '\'' + 
       ", order_user_id_number='" + order_user_id_number + '\'' + 
       ", order_id='" + order_id + '\'' + 
       '}'; 
    } 

    public OrderUser(Long order_user_id, String order_user_name, String order_user_phone, String order_user_id_number, String order_id) { 
     this.order_user_id = order_user_id; 
     this.order_user_name = order_user_name; 
     this.order_user_phone = order_user_phone; 
     this.order_user_id_number = order_user_id_number; 
     this.order_id = order_id; 
    } 

    public OrderUser() { 
    } 

} 

這裏是我的碎片

public class ListChoiceone extends Fragment { 
    private RecyclerView recyclerView; 
    private List<Order> listBean; 
    private RecyclerAdapter adapter; 
    private String url = "http://jm/user/order/getNPayedOrders?user_id=9"; 


    public ListChoiceone() { 
     // Required empty public constructor 
    } 


    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
          Bundle savedInstanceState) { 
     // Inflate the layout for this fragment 

     View view = inflater.inflate(R.layout.fragment_list_choiceone, container, false); 

     LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity()); 
     recyclerView = (RecyclerView) view.findViewById(R.id.my_recycler_view); 
     initData(); 
     adapter = new RecyclerAdapter(listBean, getActivity()); 
     recyclerView.setHasFixedSize(true); 
     recyclerView.setLayoutManager(layoutManager); 
     recyclerView.setAdapter(adapter); 
     sendRequestWithHttpClicent(); 

     return view; 


    } 

    private void sendRequestWithHttpClicent() { 
     new Thread(new Runnable() { 
      @Override 
      public void run() { 
       try { 
        OkHttpClient client = new OkHttpClient(); 
        Request request = new Request.Builder() 
          .url(url) 
          .build(); 
        Response response = client.newCall(request).execute(); 
        String responseData = response.body().string(); 
        parseJSONWithJSONObject(responseData); 

       } catch (IOException e) { 
        e.printStackTrace(); 
       } 
      } 
     }).start(); 
    } 

    private void parseJSONWithJSONObject(String jsonData) { 
     try { 
      JSONObject jsonObject = new JSONObject(jsonData); 
      String status = jsonObject.getString("status"); 
      String msg = jsonObject.getString("msg"); 

      JSONArray jsonArray = jsonObject.getJSONArray("data"); 
      for (int i = 0; i < jsonArray.length(); i++) { 
       JSONObject json = jsonArray.getJSONObject(i); 
       int order_id = json.getInt("order_id"); 

       String order_number = json.getString("order_number"); 
       String order_transactionid = json.getString("order_transactionid"); 
       String order_phone = json.getString("order_phone"); 
       String order_state = json.getString("order_state"); 
       String order_check_in_time = json.getString("order_check_in_time"); 
       String order_check_out_time = json.getString("order_check_out_time"); 
       String order_price = json.getString("order_price"); 
       String hotel_name = json.getString("hotel_name"); 
       String hotel_addres = json.getString("hotel_addres"); 
       String order_room_number = json.getString("order_room_number"); 
       String room_type_name = json.getString("room_type_name"); 

       int hotel_id = json.getInt("hotel_id"); 
       int user_id = json.getInt("user_id"); 

       JSONArray jsonArrayOrder = json.getJSONArray("orderUserList"); 
       ArrayList<OrderUser> listOrderUser = new ArrayList<OrderUser>(); 


       Log.i("nige", order_state); 

      } 

     } catch (JSONException e) { 
      e.printStackTrace(); 
     } 
    } 

    private void initData() { 
     listBean = new ArrayList<>(); 



    } 

} 

非常感謝你我的編碼器的朋友!

+1

什麼** **正是在代碼不工作,你提供的? – csmckelvey

+0

看我的回答http://stackoverflow.com/questions/41954121/how-to-use-google-gson-to-convert-a-json-string-into-java-pojo/42039142#42039142 –

+0

因爲我不得不隱藏服務器的接口代碼。 – 95DBC

回答

0

簡單的例子:

List<Model> data = new ArrayList<>(); 

JSONArray jsonArray = jsonObject.getJSONArray("data"); 

for (int i = 0; i < jsonArray.length(); i++) { 
    data.add(new Model(/*fill your data*/)); 
} 
+0

在此先感謝 – 95DBC

+0

謝謝你提醒我。 – 95DBC

+0

不客氣 –

0

移動在此之前的for循環

ArrayList<OrderUser> listOrderUser = new ArrayList<OrderUser>(); 

然後,只需使用添加項目,listOrderUser.add(<your_model_item>);

和之後的for循環,做一個notifyDataSetChanged ();在recyclerView適配器

+0

但我不會將JSONArry轉換爲ArrayList,我如何使用 – 95DBC

+0

添加項目謝謝您指出我的錯誤。 – 95DBC

-2
{"myjson":[{"name":"Rajkumar","place":"Erode","gender":"male"},{"name":"Dineshkumar","place":"Erode","gender":"male"}, 
    {"name":"Saravanakumar","place":"mettur","gender":"male"} 
    {"name":"Jayakumar","place":"chennai","gender":"male"}]} 

    List<RajObj> list = new ArrayList<>(); 
    if (op!= null) { 
     int len = myjson.length(); 
     for (int i=0; i< len; i++) { 
     JSONObject o = (JSONObject) op.get(i); 
     list.add(new RajObj(o.getString('name'), o.getString('place'), o.getString('gender'))); 
     } 
    } 
    System.out.println("Result " + list.size() + " objects."); 
    public static final class RajObj { 
     final String name; 
     final String place; 
     final String gender; 
     public RajObj(String name, String place, String gender) { 
     this.name = name; 
     this.place = place; 
     this.gender = gender; 
     } 
    } 
+0

他是要求android代碼不爲php – sodhankit

+0

嗨,你在開玩笑吧:) – 95DBC

+0

謝謝sodhankit。我會盡快更新 –