Home > OS >  How do I get the value from nested locally stored json-file in Flutter?
How do I get the value from nested locally stored json-file in Flutter?

Time:11-24

I'm trying to get the nested values from my locally stored json file with Flutter.

I can get the "outer" values, but I haven't been able to get the "inner" ones. I have googled and searched here, but I still can't make it work, so any help is much appreciated.

I put the code in a sandbox to make it easier to see. https://codesandbox.io/s/loving-thunder-meklbc?file=/lib/main.dart

If you rather look here this is what some files look like:

json:

[{
    "id":184423,
    "created":"2022-11-18T09:32:56.000Z",
    "raw_data":[        
        {"measurement_id":18,"index":0,"substance":655,"pressure":20,"temperature":30.03},
        {"measurement_id":18,"index":1,"substance":648,"pressure":38,"temperature":30.03},
        {"measurement_id":18,"index":2,"substance":636,"pressure":90,"temperature":30.02},
        {"measurement_id":18,"index":3,"substance":623,"pressure":130,"temperature":30.05},
        {"measurement_id":18,"index":4,"substance":598,"pressure":147,"temperature":29.99}
        ]
    },
    {
        "id":184423,
        "created":"2022-11-19T09:32:56.000Z",
        "raw_data":[        
            {"measurement_id":18,"index":0,"substance":586,"pressure":160,"temperature":30.05},
            {"measurement_id":18,"index":1,"substance":564,"pressure":170,"temperature":29.99},
            {"measurement_id":18,"index":2,"substance":553,"pressure":173,"temperature":30},
            {"measurement_id":18,"index":3,"substance":544,"pressure":162,"temperature":30.02},
            {"measurement_id":18,"index":4,"substance":538,"pressure":164,"temperature":30.01}
            ]
        }
]

handler:

import 'dart:convert';

import 'package:flutter/services.dart' as rootbundle;
import '../model/usermodel.dart';

Future<List<UserModel>> readJsonData() async {
  final jsondata = await rootbundle.rootBundle.loadString('/userdata.json');

  final list = json.decode(jsondata) as List<dynamic>;

  //print(list);
  return list.map((e) => UserModel.fromJson(e)).toList();
}

model:

// ignore_for_file: non_constant_identifier_names

class UserModel {
  late int? id, measurementId, index, substance, pressure;
  late double? temperature;

  UserModel(
    this.id,
    this.measurementId,
    this.index,
    this.substance,
    this.pressure,
    this.temperature,
  );

  UserModel.fromJson(Map<String, dynamic> json) {
    id = json["id"];
    measurementId = json['measurement_id'];
    index = json['index'];
    substance = json['substance'];
    pressure = json['pressure'];
    temperature = json['temperature'];
  }
}

CodePudding user response:

List<UserModel> models = [];
for (var item in list) {
  models.addAll(item.map((e) => UserModel.fromJson(e['id'], e['raw_data'])));
}
return models;

UserModel.fromJson(int id, Map<String, dynamic> json) {
  this.id = id; // parse json (raw_data)
}

CodePudding user response:


class UserModel {
  UserModel(this.id, this.raw_data);

  /// Creates a UserModel from Json map
  factory UserModel.fromJson(Map<String, dynamic> json) => UserModel(
        json['id'] as int?,
        (json['raw_data'] as List<dynamic>?)
            ?.map((e) => Data.fromJson(e as Map<String, dynamic>))
            .toList(),
      );

  final int? id;
  final List<Data>? raw_data;
}

//Data 

class Data {
  Data(
    this.measurement_id,
    this.index,
    this.substance,
    this.pressure,
    this.temperature,
  );

  final int? measurement_id;
  final int? index;
  final int? substance;
  final int? pressure;
  final double? temperature;

  /// Creates a Data from Json map
  factory Data.fromJson(Map<String, dynamic> json) => Data(
      json['measurement_id'] as int?,
      json['index'] as int?,
      json['substance'] as int?,
      json['pressure'] as int?,
      (json['temperature'] as num?)?.toDouble(),
    );
}
  • Related