我正在尝试获取以下JSONArray对象。有两个项目,但我已经实现了循环,我只能获取1项。我用了2个循环。怎么解决?有没有其他方法来获取所有数据?如果我从-1开始for循环,那么它会显示数组索引超出范围。
JSON结构如下
{
"status": 200,
"list": [
{
"quot_uid": "QUOTE2018@1",
"id": "1",
"expiry_date": "2018-05-29",
"created_at": "2018-05-22 11:45:58",
"left_days": "9",
"items": [
{
"ITEM_NAME": "Copper Wires",
"UNIT": "MT",
"qty": "5",
"make": null
},
{
"ITEM_NAME": "OFC Cables",
"UNIT": "MT",
"qty": "2",
"make": null
}
]
}
]
}
这是我已经尝试过的代码
JSONObject jsonObject = new JSONObject(response);
JSONArray jsonArray = jsonObject.getJSONArray("list");
tv.removeAllViewsInLayout();
int flag = 1;
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonObject1 = jsonArray.getJSONObject(i);
JSONArray jsonArray1 = jsonObject1.getJSONArray("items");
for (int j = 0; j < jsonArray1.length(); j++) {
TableRow tr = new TableRow(Main2Activity.this);
tr.setLayoutParams(new ViewGroup.LayoutParams(
ViewGroup.LayoutParams.FILL_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT));
if (flag == 1) {
TextView t1 = new TextView(Main2Activity.this);
t1.setPadding(10, 30, 10, 30);
t1.setText("ITEM NAME");
t1.setTextAlignment(View.TEXT_ALIGNMENT_VIEW_START);
t1.setTop(20);
t1.setTextColor(Color.BLACK);
t1.setTextSize(15);
t1.setTypeface(null, Typeface.BOLD);
tr.addView(t1);
TextView t2 = new TextView(Main2Activity.this);
t2.setPadding(10, 30, 10, 30);
t2.setText("QTY");
t2.setTextAlignment(View.TEXT_ALIGNMENT_VIEW_START);
t2.setTop(20);
t2.setTextColor(Color.BLACK);
t2.setTextSize(15);
t2.setTypeface(null, Typeface.BOLD);
tr.addView(t2);
tv.addView(tr);
final View vline = new View(Main2Activity.this);
vline.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT, 10));
vline.setBackgroundColor(Color.BLUE);
tv.addView(vline);
flag = 0;
} else {
JSONObject jsonObject2 = jsonArray1.getJSONObject(j);
TextView tv1 = new TextView(Main2Activity.this);
tv1.setPadding(5, 30, 5, 30);
String item_nm = jsonObject2.getString("ITEM_NAME");
tv1.setText(item_nm);
tv1.setTextColor(Color.BLACK);
tv1.setTextAlignment(View.TEXT_ALIGNMENT_VIEW_START);
tv1.setTextSize(15);
tr.addView(tv1);
TextView tv2 = new TextView(Main2Activity.this);
tv2.setPadding(5, 30, 5, 30);
String item_qty = jsonObject2.getString("qty");
tv2.setText(item_qty);
tv2.setTextColor(Color.BLACK);
tv2.setTextAlignment(View.TEXT_ALIGNMENT_VIEW_START);
tv2.setTextSize(15);
tr.addView(tv2);
tv.addView(tr);
final View vline1 = new View(Main2Activity.this);
vline1.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT, 1));
vline1.setBackgroundColor(Color.rgb(1, 132, 143));
tv.addView(vline1);
}
答案 0 :(得分:1)
试试这个对我有用。 我只是提供了完整的体系结构打印或使用您想要的值作为您的要求,对于我记录的几个值的样本。
主要区别在于我用过
[2018-06-07 10:41:02,849] ERROR in app: Exception on /item/test [POST]
Traceback (most recent call last):
File "C:\Python27\lib\site-packages\flask\app.py", line 1612, in full_dispatch_request
rv = self.dispatch_request()
File "C:\Python27\lib\site-packages\flask\app.py", line 1598, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "C:\Python27\lib\site-packages\flask_restful\__init__.py", line 480, in wrapper
resp = resource(*args, **kwargs)
File "C:\Python27\lib\site-packages\flask\views.py", line 84, in view
return self.dispatch_request(*args, **kwargs)
File "C:\Python27\lib\site-packages\flask_restful\__init__.py", line 595, in dispatch_request
resp = meth(*args, **kwargs)
File "G:\flask_workspace\MealBookingApp\MealBookingApp\MealBookingApp\views.py", line 30, in post
item = next(filter(lambda x: x['name'] == name, items), None)
TypeError: list object is not an iterator
127.0.0.1 - - [07/Jun/2018 10:41:02] "POST /item/test HTTP/1.1" 500 -
而不是你的行
item = next(filter(lambda x: x['name'] == name, items), None)
完整的代码框架如下。我在字符串中使用了您的JSON而不是从响应中获取,如果它有效则尝试它,然后将其更改为响应。它适用于我。
JSONObject listObj1=new JSONObject(jsonArray.get(i).toString());
检查错误日志,因为它将返回任何
的JSON解析错误