首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何在颤振中解析/迭代多个嵌套的json数组

如何在颤振中解析/迭代多个嵌套的json数组
EN

Stack Overflow用户
提问于 2022-03-09 06:09:26
回答 2查看 1.4K关注 0票数 1

我有来自restapi和wnat的响应来访问注释中的回复,我有来自quicktype.io的模型,但不知道如何迭代和获取数据。

代码语言:javascript
运行
复制
  "data": {
"_id": "61ee65fd92a48c7b10779f24",
"topicName": "Test Topic",
"description": "<p>Test Discussion</p>",
"createdByName": "Admin",
"comments": [
  {
    "_id": "622740a70fdfbb1bdd00232b",
    "comment": "post commnet test",
    "commentedById": "611cb5ecaf1a1bbc858d4d13",
    "user": [
      {
        "title": "Mr",
        "firstName": "John",
        "lastName": "Cart",
        "userID": "USER1000022",
        "_id": "611cb5ecaf1a1bbc858d4d13"
      }
    ],
    "replies": []
  },
  {
    "_id": "620d05382773643acd49177b",
    "comment": "test message",
    "commentedById": "611cb5ecaf1a1bbc858d4d13",
    "commentedByName": "Mr. John Cart",
    "user": [
      {
        "title": "Mr",
        "firstName": "John",
        "lastName": "Cart",
        "userID": "USER1000022",
        "_id": "611cb5ecaf1a1bbc858d4d13"
      }
    ],
    "replies": [
      {
        "_id": "6227351b0fdfbb099800232a",
        "comment": "test msg",
        "commentedById": "611cb5ecaf1a1bbc858d4d13",
        "commentedByName": "Mr, john cart",
        "replyUser": [
          {
            "title": "Mr",
            "firstName": "John",
            "lastName": "cart",
            "userID": "USER1000022",
            "_id": "611cb5ecaf1a1bbc858d4d13"
          }
        ]
      }
    ],
  },

任何帮助或文件/建议/指南都是有帮助的

谢谢

EN

回答 2

Stack Overflow用户

发布于 2022-03-09 06:16:54

代码语言:javascript
运行
复制
var object= json.decode(response.body)

对象变量必须采用重复语法。

代码语言:javascript
运行
复制
 print(object["data"]["comments"][index]["replies"]
票数 0
EN

Stack Overflow用户

发布于 2022-03-09 06:42:08

从internet获取数据

此菜谱使用以下步骤:

  1. 添加http包。
  2. 使用http包发出网络请求。
  3. 将响应转换为自定义Dart对象。
  4. 用颤振法获取并显示数据。

dependencies: http: <latest_version>

导入http包

代码语言:javascript
运行
复制
import 'package:http/http.dart' as http;

此外,在您的AndroidManifest.xml文件中,添加Internet权限。

代码语言:javascript
运行
复制
<!-- Required to fetch data from the internet. -->
<uses-permission android:name="android.permission.INTERNET" />

发出网络请求

代码语言:javascript
运行
复制
Future<YourModel> fetchData() async {
  final response = await http
      .get(Uri.parse('yourapiurl'));

  if (response.statusCode == 200) {
    // If the server did return a 200 OK response,
    // then parse the JSON.
    return YourModel.fromJson(jsonDecode(response.body));
  } else {
    // If the server did not return a 200 OK response,
    // then throw an exception.
    throw Exception('Failed to load data');
  }
}

获取数据

代码语言:javascript
运行
复制
class _MyAppState extends State<MyApp> {
  late Future<YourModel> yourAlbum;

  @override
  void initState() {
    super.initState();
    yourModel = fetchData();
  }
  // ···
}

显示数据

代码语言:javascript
运行
复制
FutureBuilder<YourModel>(
  future: futureModel,
  builder: (context, snapshot) {
    if (snapshot.hasData) {
      return Text(snapshot.data!.title);
    } else if (snapshot.hasError) {
      return Text('${snapshot.error}');
    }

    // By default, show a loading spinner.
    return const CircularProgressIndicator();
  },
)
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71405009

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档