获取字段下的JSON数量?

huangapple go评论82阅读模式
英文:

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
}

huangapple
  • 本文由 发表于 2023年5月13日 15:47:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/76241632.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定