我正在尝试在 dart 中解析本地 JSON 文件并显示其值。
用户.json
[
{
"id": 1,
"name": "Greg",
"imageUrl": "assets/images/greg.jpg"
},
{
"id": 2,
"name": "James",
"imageUrl": "assets/images/james.jpg"
}
]
Run Code Online (Sandbox Code Playgroud)
这是我的 chats.json
[
{
"sender": "james",
"time": "5:30 PM",
"text": "Hey, how's it going? What did you do today?",
"isLiked": false,
"unread": true
},
{
"sender": "olivia",
"time": "4:30 PM",
"text": "Hey, how's it going? What did you do today?",
"isLiked": true,
"unread": false
}
]
Run Code Online (Sandbox Code Playgroud)
这是我的消息类
class Message {
final User sender;
final String time; …Run Code Online (Sandbox Code Playgroud) 在控制台中,我似乎无法记录"男性"或"女性"的值.应该是这样的console.log(choice[0].a).它不记录变量的值string.以下是我的尝试,谢谢你的时间.
<html>
<head>
<script type="text/javascript">
var string = "";
var randoml = "ab";
while (string.length < 1) {
string += randoml[Math.floor(Math.random() * randoml.length)];
}
console.log(string);
data = '[{"a" : "male", "b" : "female"}]';
var choice = JSON.parse(data);
console.log(choice[0].string);
</script>
</head>
</html>
Run Code Online (Sandbox Code Playgroud)