英文:
How to retrive nested json array in flutter?
问题
以下是翻译好的部分:
这是我的JSON数据:
{
"result":"0",
"school_code":"学校代码",
"dashboard":{
"resultPrivilege":"0",
"privilege":[
{
"activity_id":"活动ID",
"privilege_id":"权限ID"
}
]
}
}
我想从这个JSON数据中提取privilege数组。
在终端上打印privilege数组。
如何提取privilege数组的JSON对象
这是我用来提取JSON数据的代码:
var data = json.decode(response.body);
var resultCheck = data['result'];
if (resultCheck == '0') {
var rest = data["dashboard"];
debugPrint(rest);
}
此代码的输出:
result check 0
I/flutter (18905): {"privilege":[
{"activity_id":165,"privilege_id":1568}
我想在打印privilege时输出以下格式:
I/flutter (18905):[{"activity_id":165,"privilege_id":1568}]
如何仅在终端上获取privilege数组,以及如何打印privilege的对象?
英文:
This is my JSON data:
{
"result":"0",
"school_code":"School Code",
"dashboard":{
"resultPrivilege":"0",
"privilege":[
{
"activity_id":"activity_id",
"privilege_id":"privilege_id"
}
]
}
}
I want to retrieve privilege array from this Json data.<br>
Print privilege array on terminal.<br>
How to retrieve JSON object of privilege array
This is my code to retrieve JSON data:
var data = json.decode(response.body);
var resultCheck = data['result'];
if (resultCheck == '0') {
var rest = data["dashboard"];
debugPrint(rest);
}
output of this code:
result check 0
I/flutter (18905): {"privilege":[
{"activity_id":165,"privilege_id":1568}
I want to output this format when i print privilege:
I/flutter (18905):[{"activity_id":165,"privilege_id":1568}]
How to get only privilege array on terminal also how to print object of privilege?.
答案1
得分: 1
Map previlege = {
'result': '0',
'school_code': '学校代码',
'dashboard': {
'resultPrivilege': '0',
'privilege': [
{
'activity_id': '活动ID',
'privilege_id': '权限ID',
}
],
}
};
print('结果: ${previlege['result']}' +
'\n学校代码: ${previlege['school_code']}' +
'\n活动ID: ${previlege['dashboard']['privilege'][0]['activity_id']}');
<details>
<summary>英文:</summary>
Map previlege = {
'result': '0',
'school_code': 'School Code',
'dashboard': {
'resultPrivilege': '0',
'privilege': [
{
'activity_id': 'activity_id',
'privilege_id': 'privilege_id',
}
],
}
};
print('Reuslt: ${previlege['result']}' +
'\nSchool Code: ${previlege['school_code']}' +
'\nActivity ID: ${previlege['dashboard']['privilege'][0]['activity_id']}');
[![Screenshot][1]][1]
[1]: https://i.stack.imgur.com/6sgkX.jpg
</details>
# 答案2
**得分**: 1
```dart
import 'dart:convert';
void main() {
var jsonData = '{"result":"0","school_code":"学校代码","dashboard":{"resultPrivilege":"0","privilege":[{"activity_id":"活动ID","privilege_id":"权限ID"}]}}';
var json = jsonDecode(jsonData);
json['dashboard']['privilege'].forEach((obj){
//
print(obj.toString());
});
}
英文:
import 'dart:convert';
void main() {
var jsonData='{"result":"0","school_code":"School Code","dashboard":{"resultPrivilege":"0", "privilege": [{"activity_id":"activity_id","privilege_id":"privilege_id"}]}}';
var json=jsonDecode(jsonData);
json['dashboard']['privilege'].forEach((obj){
//
print(obj.toString());
});
}
答案3
得分: 0
尝试这个,
Map<String, dynamic> jsonBody = json.decode(response.body);
String result = jsonBody["result"];
if (result == '0') {
Map<String, dynamic> dashboard = jsonBody["dashboard"];
List privilege = dashboard["privilege"];
debugPrint(privilege.toString());
}
或者你可以使用quicktype或JsonToDart在线工具轻松创建PODO(普通的Dart对象)类,然后你可以将JSON转换为Dart类对象。
英文:
Try this,
Map<String, dynamic> jsonBody = json.decode(response.body);
String result = jsonBody["result"];
if (result == '0') {
Map<String, dynamic> dashboard = jsonBody["dashboard"];
List privilege = dashboard["privilege"];
debugPrint(privilege.toString());
}
or you can easily create PODO(Plain Old Dart Objects) classes using quicktype or JsonToDart online tools. Then you can convert JSON to dart class objects.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论