如何在 Android Studio 中使用截击将 JSON 数组值设置为设置器 class
How to set JSON Array values to seter class in Android Studio with volley
我正在尝试在 ListView
的数据库中显示我的行表,但是我不知道如何将 JSONObject
转换为 JSONArray
并将其设置为我的 setter class。我通过 Volley String Request
得到我的 JSON
我有这样的 JSON 输出:
{
"error": false,
"message": "Status Fetched",
"status": [
{
"OrderCode": "5x2azu",
"GuestName": "Try",
"ProductName": "Mie Ayam Super Jumbo Komplit",
"ProductType": "Kuah",
"NoTable": 4,
"Status": "Disiapkan",
"TotalPrice": "58000"
},
{
"OrderCode": "etent3",
"GuestName": "Try",
"ProductName": "Nasi Soto Daging Sapi",
"ProductType": "Soto",
"NoTable": 4,
"Status": "Disiapkan",
"TotalPrice": "27000"
},
{
"OrderCode": "ro1eyx",
"GuestName": "Try",
"ProductName": "Mie Ayam Original",
"ProductType": "Kuah",
"NoTable": 4,
"Status": "Disiapkan",
"TotalPrice": "23000"
}
]
}
这是我的字符串请求:
public void StringRequest() {
StringRequest stringRequest = new StringRequest(Request.Method.POST, URLs.STATUS,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try {
JSONObject obj = new JSONObject(response);
Toast.makeText(getContext(), response, Toast.LENGTH_SHORT).show();
//if no error in response
if (!obj.getBoolean("error")) {
Toast.makeText(getContext(), obj.getString("message"), Toast.LENGTH_SHORT).show();
//getting Status from response
JSONObject statusJson = obj.getJSONObject("status");
Status status = new Status(
statusJson.getString("OrderCode"),
statusJson.getString("GuestName"),
statusJson.getString("ProductName"),
statusJson.getString("ProductType"),
statusJson.getString("NoTable"),
statusJson.getString("Status"),
statusJson.getString("TotalPrice")
);
} else {
Toast.makeText(getContext(), obj.getString("message"), Toast.LENGTH_SHORT).show();
}
} catch (JSONException e) {
e.printStackTrace();
}
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(getContext(),error.getMessage(),Toast.LENGTH_SHORT).show();
}
}) {
@Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String,String> params = new HashMap<>();
params.put("tablecode",TableCode);
return params;
}
};
VolleySingleton.getInstance(getContext()).addToRequestQueue(stringRequest);
}
我不知道如何将它从 JSONObject
转换为 JSONArray
这是我的 Setter 和 getter class
package com.example.pesanpalgading20.Getter.Status;
import java.util.ArrayList;
public class Status {
private String OrderCode,Name,FoodName,TypeFood,NoTable,Status,TotalPrice;
public Status(){
}
public Status(String orderCode, String name, String foodName, String typeFood, String noTable, String status, String totalPrice) {
OrderCode = orderCode;
Name = name;
FoodName = foodName;
TypeFood = typeFood;
NoTable = noTable;
Status = status;
TotalPrice = totalPrice;
}
public String getOrderCode() {
return OrderCode;
}
public void setOrderCode(String orderCode) {
OrderCode = orderCode;
}
public String getName() {
return Name;
}
public void setName(String name) {
Name = name;
}
public String getFoodName() {
return FoodName;
}
public void setFoodName(String foodName) {
FoodName = foodName;
}
public String getTypeFood() {
return TypeFood;
}
public void setTypeFood(String typeFood) {
TypeFood = typeFood;
}
public String getNoTable() {
return NoTable;
}
public void setNoTable(String noTable) {
NoTable = noTable;
}
public String getStatus() {
return Status;
}
public void setStatus(String status) {
Status = status;
}
public String getTotalPrice() {
return TotalPrice;
}
public void setTotalPrice(String totalPrice) {
TotalPrice = totalPrice;
}
}
那么如何将每个 JSON 的值设置为 SetterandGetter class ?
try {
JSONObject obj = new JSONObject(response);
//if no error in response
if (!obj.getBoolean("error")) {
//getting Status from response
JSONArray statusJson = obj.getJSONArray("status");
for (int i = 0; i < statusJson.length(); i++) {
Status status = new Status(
statusJson.getJSONObject(i).getString("OrderCode"),
statusJson.getJSONObject(i).getString("GuestName"),
statusJson.getJSONObject(i).getString("ProductName"),
statusJson.getJSONObject(i).getString("ProductType"),
statusJson.getJSONObject(i).getString("NoTable"),
statusJson.getJSONObject(i).getString("Status"),
statusJson.getJSONObject(i).getString("TotalPrice")
);
// Add Status In your Array list and enjoy
}
} else {
Toast.makeText(getContext(), obj.getString("message"), Toast.LENGTH_SHORT).show();
}
} catch (JSONException e) {
e.printStackTrace();
}
您可以使用 Gson 库并将您的 json 转换为模型 class
首先你必须添加 gson 作为依赖项
implementation "com.google.code.gson:gson:2.8.6"
然后因为你在 json 中的状态是数组,所以像这样使用它
JSONArray status = obj.getJSONArray("status");
//Code to convert json array to arraylist of object
Arraylist<Status> arraylist = Gson().fromJson(
status.toString(),
new TypeToken<ArrayList<Status>>(){}.getType()
)
以上代码会将您的 json 数组状态存储到 arraylist 变量
所以 gettin JSON 数组已经完成,但它仍然显示数组中的一个列表这是代码
public void StringRequest() {
StringRequest stringRequest = new StringRequest(Request.Method.POST, URLs.STATUS,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try {
JSONObject obj = new JSONObject(response);
Toast.makeText(getContext(), response, Toast.LENGTH_SHORT).show();
//if no error in response
if (!obj.getBoolean("error")) {
Toast.makeText(getContext(), obj.getString("message"), Toast.LENGTH_SHORT).show();
//getting Status from response
JSONArray statusJson = obj.getJSONArray("status");
for (int i = 0; i < statusJson.length(); i++) {
Status status = new Status(
statusJson.getJSONObject(i).getString("OrderCode"),
statusJson.getJSONObject(i).getString("GuestName"),
statusJson.getJSONObject(i).getString("ProductName"),
statusJson.getJSONObject(i).getString("ProductType"),
statusJson.getJSONObject(i).getString("NoTable"),
statusJson.getJSONObject(i).getString("Status"),
statusJson.getJSONObject(i).getString("TotalPrice")
);
ArrayList<Status> statusList = new ArrayList<Status>();
statusList.add(status);
statusAdapter = new StatusAdapter(getActivity(),statusList);
StatusListView.setAdapter(statusAdapter);
}
} else {
Toast.makeText(getContext(), obj.getString("message"), Toast.LENGTH_SHORT).show();
}
} catch (JSONException e) {
e.printStackTrace();
}
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(getContext(),error.getMessage(),Toast.LENGTH_SHORT).show();
}
}) {
@Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String,String> params = new HashMap<>();
params.put("tablecode",TableCode);
return params;
}
};
VolleySingleton.getInstance(getContext()).addToRequestQueue(stringRequest);
}
我不知道把设置适配器中的增量放在哪里
statusList.add(status);
编辑:解决了!感谢@Pratik Fagadiya
我将设置适配器放在循环之外并且它起作用并添加了超过 1 个列表
我正在尝试在 ListView
的数据库中显示我的行表,但是我不知道如何将 JSONObject
转换为 JSONArray
并将其设置为我的 setter class。我通过 Volley String Request
我有这样的 JSON 输出:
{
"error": false,
"message": "Status Fetched",
"status": [
{
"OrderCode": "5x2azu",
"GuestName": "Try",
"ProductName": "Mie Ayam Super Jumbo Komplit",
"ProductType": "Kuah",
"NoTable": 4,
"Status": "Disiapkan",
"TotalPrice": "58000"
},
{
"OrderCode": "etent3",
"GuestName": "Try",
"ProductName": "Nasi Soto Daging Sapi",
"ProductType": "Soto",
"NoTable": 4,
"Status": "Disiapkan",
"TotalPrice": "27000"
},
{
"OrderCode": "ro1eyx",
"GuestName": "Try",
"ProductName": "Mie Ayam Original",
"ProductType": "Kuah",
"NoTable": 4,
"Status": "Disiapkan",
"TotalPrice": "23000"
}
]
}
这是我的字符串请求:
public void StringRequest() {
StringRequest stringRequest = new StringRequest(Request.Method.POST, URLs.STATUS,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try {
JSONObject obj = new JSONObject(response);
Toast.makeText(getContext(), response, Toast.LENGTH_SHORT).show();
//if no error in response
if (!obj.getBoolean("error")) {
Toast.makeText(getContext(), obj.getString("message"), Toast.LENGTH_SHORT).show();
//getting Status from response
JSONObject statusJson = obj.getJSONObject("status");
Status status = new Status(
statusJson.getString("OrderCode"),
statusJson.getString("GuestName"),
statusJson.getString("ProductName"),
statusJson.getString("ProductType"),
statusJson.getString("NoTable"),
statusJson.getString("Status"),
statusJson.getString("TotalPrice")
);
} else {
Toast.makeText(getContext(), obj.getString("message"), Toast.LENGTH_SHORT).show();
}
} catch (JSONException e) {
e.printStackTrace();
}
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(getContext(),error.getMessage(),Toast.LENGTH_SHORT).show();
}
}) {
@Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String,String> params = new HashMap<>();
params.put("tablecode",TableCode);
return params;
}
};
VolleySingleton.getInstance(getContext()).addToRequestQueue(stringRequest);
}
我不知道如何将它从 JSONObject
转换为 JSONArray
这是我的 Setter 和 getter class
package com.example.pesanpalgading20.Getter.Status;
import java.util.ArrayList;
public class Status {
private String OrderCode,Name,FoodName,TypeFood,NoTable,Status,TotalPrice;
public Status(){
}
public Status(String orderCode, String name, String foodName, String typeFood, String noTable, String status, String totalPrice) {
OrderCode = orderCode;
Name = name;
FoodName = foodName;
TypeFood = typeFood;
NoTable = noTable;
Status = status;
TotalPrice = totalPrice;
}
public String getOrderCode() {
return OrderCode;
}
public void setOrderCode(String orderCode) {
OrderCode = orderCode;
}
public String getName() {
return Name;
}
public void setName(String name) {
Name = name;
}
public String getFoodName() {
return FoodName;
}
public void setFoodName(String foodName) {
FoodName = foodName;
}
public String getTypeFood() {
return TypeFood;
}
public void setTypeFood(String typeFood) {
TypeFood = typeFood;
}
public String getNoTable() {
return NoTable;
}
public void setNoTable(String noTable) {
NoTable = noTable;
}
public String getStatus() {
return Status;
}
public void setStatus(String status) {
Status = status;
}
public String getTotalPrice() {
return TotalPrice;
}
public void setTotalPrice(String totalPrice) {
TotalPrice = totalPrice;
}
}
那么如何将每个 JSON 的值设置为 SetterandGetter class ?
try {
JSONObject obj = new JSONObject(response);
//if no error in response
if (!obj.getBoolean("error")) {
//getting Status from response
JSONArray statusJson = obj.getJSONArray("status");
for (int i = 0; i < statusJson.length(); i++) {
Status status = new Status(
statusJson.getJSONObject(i).getString("OrderCode"),
statusJson.getJSONObject(i).getString("GuestName"),
statusJson.getJSONObject(i).getString("ProductName"),
statusJson.getJSONObject(i).getString("ProductType"),
statusJson.getJSONObject(i).getString("NoTable"),
statusJson.getJSONObject(i).getString("Status"),
statusJson.getJSONObject(i).getString("TotalPrice")
);
// Add Status In your Array list and enjoy
}
} else {
Toast.makeText(getContext(), obj.getString("message"), Toast.LENGTH_SHORT).show();
}
} catch (JSONException e) {
e.printStackTrace();
}
您可以使用 Gson 库并将您的 json 转换为模型 class
首先你必须添加 gson 作为依赖项
implementation "com.google.code.gson:gson:2.8.6"
然后因为你在 json 中的状态是数组,所以像这样使用它
JSONArray status = obj.getJSONArray("status");
//Code to convert json array to arraylist of object
Arraylist<Status> arraylist = Gson().fromJson(
status.toString(),
new TypeToken<ArrayList<Status>>(){}.getType()
)
以上代码会将您的 json 数组状态存储到 arraylist 变量
所以 gettin JSON 数组已经完成,但它仍然显示数组中的一个列表这是代码
public void StringRequest() {
StringRequest stringRequest = new StringRequest(Request.Method.POST, URLs.STATUS,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try {
JSONObject obj = new JSONObject(response);
Toast.makeText(getContext(), response, Toast.LENGTH_SHORT).show();
//if no error in response
if (!obj.getBoolean("error")) {
Toast.makeText(getContext(), obj.getString("message"), Toast.LENGTH_SHORT).show();
//getting Status from response
JSONArray statusJson = obj.getJSONArray("status");
for (int i = 0; i < statusJson.length(); i++) {
Status status = new Status(
statusJson.getJSONObject(i).getString("OrderCode"),
statusJson.getJSONObject(i).getString("GuestName"),
statusJson.getJSONObject(i).getString("ProductName"),
statusJson.getJSONObject(i).getString("ProductType"),
statusJson.getJSONObject(i).getString("NoTable"),
statusJson.getJSONObject(i).getString("Status"),
statusJson.getJSONObject(i).getString("TotalPrice")
);
ArrayList<Status> statusList = new ArrayList<Status>();
statusList.add(status);
statusAdapter = new StatusAdapter(getActivity(),statusList);
StatusListView.setAdapter(statusAdapter);
}
} else {
Toast.makeText(getContext(), obj.getString("message"), Toast.LENGTH_SHORT).show();
}
} catch (JSONException e) {
e.printStackTrace();
}
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(getContext(),error.getMessage(),Toast.LENGTH_SHORT).show();
}
}) {
@Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String,String> params = new HashMap<>();
params.put("tablecode",TableCode);
return params;
}
};
VolleySingleton.getInstance(getContext()).addToRequestQueue(stringRequest);
}
我不知道把设置适配器中的增量放在哪里
statusList.add(status);
编辑:解决了!感谢@Pratik Fagadiya 我将设置适配器放在循环之外并且它起作用并添加了超过 1 个列表