[英]How can I access the data inside the json
這是我來自 JSON 文件的數據。
[
{
"id": "5",
"msg": {
"msg": "2121222",
"sign": "אלמוג שטרית",
"time": "26/6/2020 16:52",
"title": "1212"
}
},
{
"id": "4",
"msg": {
"msg": "דשחדש",
"sign": "אלמוג שטרית",
"time": " ",
"title": "בדיקה נ"
}
},
{
"id": "3",
"msg": {
"msg": "21",
"sign": "אלמוג שטרית",
"time": "",
"title": "בדיקה חדשה"
}
},
{
"id": "2",
"msg": {
"msg": "בדיקה חדשה",
"sign": "אלמוג שטרית",
"time": "",
"title": "בדיקה חדשה"
}
},
{
"id": "1",
"msg": {
"msg": "בדיקה",
"sign": "",
"time": "",
"title": "בדיקה"
}
}
]
我使用 PHP 從哪個數據庫中提取這些數據。 php 代碼:
$stm = $conn->prepare($query);
$stm->execute();
$rows = $stm->fetchAll(PDO::FETCH_ASSOC);
$js = json_encode($rows, JSON_UNESCAPED_UNICODE);
print $js;
我的問題是如何將“msg”示例中包含的數據訪問到“title”中。
我在 flutter 上的代碼是:
List data;
Future<String> getData() async {
var response = await http.get(
Uri.encodeFull("*myUrl*"),
headers: {
"Accept": "application/json"
}
);
this.setState(() {
data = json.decode(response.body);
});
print(data[0]["title"]);
return "Success!";
}
看看這個例子:
[
{
"id": "5",
"msg": {
"msg": "2121222",
"sign": "אלמוג שטרית",
"time": "26/6/2020 16:52",
"title": "1212"
}
},
{
"id": "4",
"msg": {
"msg": "דשחדש",
"sign": "אלמוג שטרית",
"time": " ",
"title": "בדיקה נ"
}
},
{
"id": "3",
"msg": {
"msg": "21",
"sign": "אלמוג שטרית",
"time": "",
"title": "בדיקה חדשה"
}
},
{
"id": "2",
"msg": {
"msg": "בדיקה חדשה",
"sign": "אלמוג שטרית",
"time": "",
"title": "בדיקה חדשה"
}
},
{
"id": "1",
"msg": {
"msg": "בדיקה",
"sign": "",
"time": "",
"title": "בדיקה"
}
}
]
以下是您提供的 json 的 model class。
// To parse this JSON data, do
//
// final yourModel = yourModelFromJson(jsonString);
import 'dart:convert';
List<YourModel> yourModelFromJson(String str) => List<YourModel>.from(json.decode(str).map((x) => YourModel.fromJson(x)));
String yourModelToJson(List<YourModel> data) => json.encode(List<dynamic>.from(data.map((x) => x.toJson())));
class YourModel {
YourModel({
this.id,
this.msg,
});
String id;
Msg msg;
factory YourModel.fromJson(Map<String, dynamic> json) => YourModel(
id: json["id"],
msg: Msg.fromJson(json["msg"]),
);
Map<String, dynamic> toJson() => {
"id": id,
"msg": msg.toJson(),
};
}
class Msg {
Msg({
this.msg,
this.sign,
this.time,
this.title,
});
String msg;
String sign;
String time;
String title;
factory Msg.fromJson(Map<String, dynamic> json) => Msg(
msg: json["msg"],
sign: json["sign"],
time: json["time"],
title: json["title"],
);
Map<String, dynamic> toJson() => {
"msg": msg,
"sign": sign,
"time": time,
"title": title,
};
}
這是主界面
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:json_parsing_example/models.dart';
void main() => runApp(MyApp());
class MyApp extends StatefulWidget {
@override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
bool _isLoading = false;
List<YourModel> yourModel = List();
Future<String> loadFromAssets() async {
return await rootBundle.loadString('json/parse.json');
}
@override
void initState() {
super.initState();
dataLoadFunction();
}
dataLoadFunction() async {
setState(() {
_isLoading = true;
});
/* String jsonString = await loadFromAssets();
// i have locally addded the data using the json file you can fetch the api over here.
final yourdataModel = yourModelFromJson(jsonString);
yourModel = yourdataModel;
Instead of this you can use the below as the above is the sample example that i have created for you
*/
/*
This is what you should do just add the url and its done.
var response = await http.get(
Uri.encodeFull("*myUrl*"),
headers: {
"Accept": "application/json"
}
);
final yourdataModel = yourModelFromJson(response.body);
yourModel = yourdataModel;
//just uncomment this code and add the url you are good to go.
*/
setState(() {
_isLoading = false;
});
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
body: _isLoading
? CircularProgressIndicator()
: ListView.builder(
itemCount: yourModel.length,
shrinkWrap: true,
itemBuilder: (BuildContext context, int index) {
return Card(
child: Column(
children: <Widget>[
Padding(
padding: const EdgeInsets.only(top: 10, left: 15),
child: Row(
children: <Widget>[
Text('Message :'),
Text(yourModel[index].msg.msg),
],
),
),
Divider(
color: Colors.grey,
),
],
),
);
},
),
),
);
}
}
只需查看示例並讓我知道它是否有效。
嘗試這個:
print(data['msg']['title'].toString());
或者這個:(如果上面的 Jason Data 不是列表)
print(data[0]['msg']['title'].toString());
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.