如何在Flutter中检索嵌套的JSON数组?

huangapple go评论80阅读模式
英文:

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[&#39;result&#39;];
    if (resultCheck == &#39;0&#39;) {
      var rest = data[&quot;dashboard&quot;];
      debugPrint(rest);
}

output of this code:

result check 0
I/flutter (18905): {&quot;privilege&quot;:[
{&quot;activity_id&quot;:165,&quot;privilege_id&quot;:1568}

I want to output this format when i print privilege:

I/flutter (18905):[{&quot;activity_id&quot;:165,&quot;privilege_id&quot;: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']}');

如何在Flutter中检索嵌套的JSON数组?


<details>
<summary>英文:</summary>

    Map previlege = {
        &#39;result&#39;: &#39;0&#39;,
        &#39;school_code&#39;: &#39;School Code&#39;,
        &#39;dashboard&#39;: {
          &#39;resultPrivilege&#39;: &#39;0&#39;,
          &#39;privilege&#39;: [
            {
              &#39;activity_id&#39;: &#39;activity_id&#39;,
              &#39;privilege_id&#39;: &#39;privilege_id&#39;,
            }
          ],
        }
      };
      print(&#39;Reuslt:  ${previlege[&#39;result&#39;]}&#39; +
          &#39;\nSchool Code:  ${previlege[&#39;school_code&#39;]}&#39; +
          &#39;\nActivity ID:  ${previlege[&#39;dashboard&#39;][&#39;privilege&#39;][0][&#39;activity_id&#39;]}&#39;);

[![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 &#39;dart:convert&#39;;

void main() {
  var jsonData=&#39;{&quot;result&quot;:&quot;0&quot;,&quot;school_code&quot;:&quot;School Code&quot;,&quot;dashboard&quot;:{&quot;resultPrivilege&quot;:&quot;0&quot;, &quot;privilege&quot;: [{&quot;activity_id&quot;:&quot;activity_id&quot;,&quot;privilege_id&quot;:&quot;privilege_id&quot;}]}}&#39;;
  var json=jsonDecode(jsonData);
  json[&#39;dashboard&#39;][&#39;privilege&#39;].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());
}

或者你可以使用quicktypeJsonToDart在线工具轻松创建PODO(普通的Dart对象)类,然后你可以将JSON转换为Dart类对象。

英文:

Try this,

Map&lt;String, dynamic&gt; jsonBody = json.decode(response.body);

String result = jsonBody[&quot;result&quot;];
if (result == &#39;0&#39;) {
  Map&lt;String, dynamic&gt; dashboard = jsonBody[&quot;dashboard&quot;];
  List privilege = dashboard[&quot;privilege&quot;];
  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.

huangapple
  • 本文由 发表于 2020年1月3日 19:31:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/59577873.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定