2

This is a flutter/dart question, in case you want to ask.

I have the following JSONObject:

{
  "header": [
    2223,
    2224
  ],
  "2223": {
    "L": 3,
    "A": 6,
    "B": 5,
    "Mode": 15,
  },
  "2224": {
    "L": 9,
    "A": 6,
    "B": 1,
    "Mode": 16,
  }
}

First I load the 'table of contents' from the header object.

var tagsJson = jsonDecode(jsonContent)['header'];
List<String> timestamps = tagsJson != null ? List.from(tagsJson) : null;

And here is my Result Object I want to assign the values to

  class Result {


  double L;
  double A;
  double B;
  int mode;

  Result({this.L, this.A, this.B, this.mode});


  void dump()
  {
    print(this.L);
    print(this.A);
    print(this.B);
    print(this.mode);
  }
}

How can I get those values of the timestamp JSONObjects, and assign them the corresponding Result Object using flutter?

3 Answers 3

1

When you do

var tagsJson = jsonDecode(jsonContent)['header'];

You are dumping the rest of the JSON value out the window. Instead, cache the parsed JSON and refer to it separately:

final parsedJson = jsonDecode(jsonContent);
final tags = parsedJson['header'];
List<String> timestamps = tags != null ? List.from(tags) : null;

It will also be easier and cleaner if you create a fromJson factory constructor for Result:

class Result {
  ...

  factory Result.fromJson(Map<String, dynamic> jsonData) {
    double l = (jsonData['L'] ?? 0.0) as double;
    double a = (jsonData['A'] ?? 0.0) as double;
    double b = (jsonData['B'] ?? 0.0) as double;
    int mode = (jsonData['Mode'] ?? 0) as int;

    return Result(L: l, A: a, B: b, mode: mode);
  }

  ...
}

Now you can iterate through timestamps and create your Results:

List<Result> results = [];
for (var timestamp in timestamps) {
  final jsonData = parsedJson[timestamp];
  if (jsonData != null) {
    results.add(Result.fromJson(jsonData));
  }
}
Sign up to request clarification or add additional context in comments.

Comments

1

Quick answer is to use this library https://github.com/k-paxian/dart-json-mapper to map your json onto your Dart classes.

"B*" ? is this for a purpose, or typo? anyway here is the beautiful code for your case, no boilerplate, no pain, no loops, no logic. And a bonus you are able to dump your model back to json string in one line of code.

// Here is the result model classes

@jsonSerializable
class ResultItem {
  num L;
  num A;
  num B;
  int Mode;
}

@jsonSerializable
class Result {
  List<num> header = [];

  final Map<String, dynamic> _itemsMap = {};

  @jsonProperty
  void setItem(String name, dynamic value) {
    _itemsMap[name] = JsonMapper.fromMap<ResultItem>(value);
  }

  @jsonProperty
  Map<String, dynamic> getItemsMap() {
    return _itemsMap;
  }
}

// here is the input json

      final json = '''
      {
        "header": [2223, 2224],
        "2223": {
                  "L": 3,
                  "A": 6,
                  "B": 5,
                  "Mode": 15
                },
        "2224": {
                  "L": 9,
                  "A": 6,
                  "B": 1,
                  "Mode": 16
                }
      }
 ''';

  // Now you have an result model instance populated from json in one line of code
  final resultInstance = JsonMapper.deserialize<Result>(json);

  // Now you have a result model instance back to json in one line of code
  print(JsonMapper.serialize(resultInstance));

Console output will be

{
 "header": [
  2223,
  2224
 ],
 "2223": {
  "L": 3,
  "A": 6,
  "B": 5,
  "Mode": 15
 },
 "2224": {
  "L": 9,
  "A": 6,
  "B": 1,
  "Mode": 16
 }
}

1 Comment

yes, the object stores L* A* B*-Color values. I didn't want to use the asterisk in json, I must have missed it.
1

Create this function in your Result Object

    /// Creates a [Result] model
    /// from a valid JSON Object
    ///
    factory Result.fromJson(Map<String, dynamic> json) => Result(
        L: json['L'],
        M: json['M'],
        B: json['B'],
        mode: json['Mode],
    );

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.