英文:
How to get the number of jsons under the field?
问题
这是一个类似的JSON结构。不是一个包含多个JSON的列表,而是一个具有唯一标识符的独立JSON。
{
"intro": {
...
},
"data": {
"1001": {
"id": 9,
"value": 115
},
...
"1009": {
"id": 7,
"value": 410
}
}
}
很容易获取data
的值,但如何获取data
中最后一个JSON的键名(即1009
)呢?
英文:
There is a json like this. It's not a list of jsons inside but rather a separate json on unique identifier.
{
"intro": {
...
},
"data": {
"1001": {
"id": 9,
"value": 115
},
...
"1009": {
"id": 7,
"value": 410
}
}
}
It's easy to get the value of data
but how to get a name of a key for the last json in data
(i.e. it is 1009
) ?
答案1
得分: 0
以下是您要翻译的代码部分:
for get a name of a key for the last json in data:
import 'dart:convert';
void main() {
final jsonString = '''
{
"intro": {},
"data": {
"1001": {
"id": 9,
"value": 115
},
"1002": {
"id": 7,
"value": 410
},
"1009": {
"id": 7,
"value": 410
}
}
}
''';
final jsonMap = json.decode(jsonString);
final lastKey = jsonMap['data'].keys.last;
print(lastKey); // getting lastkey value is "1009"
}
for get the number of jsons under the field?
import 'dart:convert';
void main() {
final jsonString = '''
{
"intro": {},
"data": {
"1001": {
"id": 9,
"value": 115
},
"1002": {
"id": 7,
"value": 410
},
"1009": {
"id": 7,
"value": 410
}
}
}
''';
final jsonMap = json.decode(jsonString);
final dataMap = jsonMap['data'];
final numberOfJsons = dataMap.length;
print(numberOfJsons); // getting number of json's length is 3
}
请注意,这只是代码的翻译部分,不包括问题或其他内容。
英文:
for get a name of a key for the last json in data:
import 'dart:convert';
void main() {
final jsonString = '''
{
"intro": {},
"data": {
"1001": {
"id": 9,
"value": 115
},
"1002": {
"id": 7,
"value": 410
},
"1009": {
"id": 7,
"value": 410
}
}
}
''';
final jsonMap = json.decode(jsonString);
final lastKey = jsonMap['data'].keys.last;
print(lastKey); // getting lastkey value is "1009"
}
for get the number of jsons under the field?
import 'dart:convert';
void main() {
final jsonString = '''
{
"intro": {},
"data": {
"1001": {
"id": 9,
"value": 115
},
"1002": {
"id": 7,
"value": 410
},
"1009": {
"id": 7,
"value": 410
}
}
}
''';
final jsonMap = json.decode(jsonString);
final dataMap = jsonMap['data'];
final numberOfJsons = dataMap.length;
print(numberOfJsons); // getting number of json's length is 3
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论