This is my data from the JSON file.
[
{
"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": "בדיקה"
}
}
]
I extract this data from what database using PHP. php code:
$stm = $conn->prepare($query);
$stm->execute();
$rows = $stm->fetchAll(PDO::FETCH_ASSOC);
$js = json_encode($rows, JSON_UNESCAPED_UNICODE);
print $js;
My question is how I can access the data contained within the "msg" example into "title".
My code on flutter is:
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!";
}
Just check out this example:
[
{
"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": "בדיקה"
}
}
]
Following is the model class for the json you provided.
// 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,
};
}
This is the main ui
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,
),
],
),
);
},
),
),
);
}
}
Just check out the example and let me know if it works.
Try this:
print(data['msg']['title'].toString());
Or this: (In Case if above Jason Data isn't a List)
print(data[0]['msg']['title'].toString());
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.