Access nested objects in json using json_serializable in Dart - json

Trying to convert my json to objects in Dart/Flutter using the json_serializable. I cannot seem to find a way to access a nested ID (data is coming from MongoDB thus the $ in the json).
Here is the json:
{
"_id": {
"$oid": "5c00b227" <-- this is what I am trying to access
},
"base": 1,
"tax": 1,
"minimum": 5,
"type": "blah"
}
Result:
class Thing {
final int id;
final String base;
final String tax;
final String type;
final int minimum;
}

It is not possible with json_serializable package itself. You have to create separate objects for getting this nested data.
Look the discussion here
https://github.com/google/json_serializable.dart/issues/490
But, there is possible way to get nested fields with added converter (solution was found here https://github.com/google/json_serializable.dart/blob/master/example/lib/nested_values_example.dart)
import 'package:json_annotation/json_annotation.dart';
part 'nested_values_example.g.dart';
/// An example work-around for
/// https://github.com/google/json_serializable.dart/issues/490
#JsonSerializable()
class NestedValueExample {
NestedValueExample(this.nestedValues);
factory NestedValueExample.fromJson(Map<String, dynamic> json) =>
_$NestedValueExampleFromJson(json);
#_NestedListConverter()
#JsonKey(name: 'root_items')
final List<String> nestedValues;
Map<String, dynamic> toJson() => _$NestedValueExampleToJson(this);
}
class _NestedListConverter
extends JsonConverter<List<String>, Map<String, dynamic>> {
const _NestedListConverter();
#override
List<String> fromJson(Map<String, dynamic> json) => [
for (var e in json['items'] as List)
(e as Map<String, dynamic>)['name'] as String
];
#override
Map<String, dynamic> toJson(List<String> object) => {
'items': [
for (var item in object) {'name': item}
]
};
}

try this,
class Thing {
int id;
String base;
String tax;
String type;
int minimum;
Thing({
this.id,
this.base,
this.tax,
this.type,
this.minimum,
});
factory Thing.fromJson(Map<String, dynamic> json) {
return Thing(
id: json['_id']["oid"],
base: json['base'].toString(),
tax: json['tax'].toString(),
type: json['type'].toString(),
minimum: json['minimum'],
);
}
}

Related

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

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":19,"index":0,"substance":586,"pressure":160,"temperature":30.05},
{"measurement_id":19,"index":1,"substance":564,"pressure":170,"temperature":29.99},
{"measurement_id":19,"index":2,"substance":553,"pressure":173,"temperature":30},
{"measurement_id":19,"index":3,"substance":544,"pressure":162,"temperature":30.02},
{"measurement_id":19,"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'];
}
}
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(),
);
}
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)
}

How to encode a list of Objects to Json in Flutter?

I have a class A that contains a list of objects of another class B(Composition). Now, I want to store the object of class A to sqlite database. I learnt how to encode basic strings or integers to json but still could not find a way to encode a list of objects.
Need to save an object of concrete 'Layout'.
class MallardDuck extends Duck {
var image = "https://image.shutterstock.com/image-vector/cartoon-duck-swimming-600w-366901346.jpg";
Widget d_widget;
List<Widget> previous_states = [];
List<Widget> redo_states = [];
}
abstract class Layout extends Duck{
List<Duck> listOfDucks = [];
bool default_layout;
}
This might help you.
It is not the answer to your specific case but hopefully this code will help you see how I did it in a different class.
class BlogData {
final String title;
final String associatedBusinessId;
final String category;
final List<BlogParagraph> blogParagraphs;
BlogData(
{this.title,
this.associatedBusinessId,
this.category,
this.blogParagraphs});
Map<String, dynamic> toJson() {
return {
'title': this.title,
'associatedBusinessId': this.associatedBusinessId,
'category': this.category,
'blogParagraphs':
blogParagraphs.map((paragraph) => paragraph.toJson()).toList()
};
}
}
Then, in the blog paragraphs class:
class BlogParagraph {
final int paragraphId;
final String content;
final Set<int> imageRefs;
BlogParagraph({this.paragraphId, this.content, this.imageRefs});
Map<String, dynamic> toJson() {
return {
'id': this.paragraphId,
'content': this.content,
'imageRefs': imageRefs.isEmpty ? [] : imageRefs.toList(),
};
}
}

Dart: converting _InternalLinkedHashMap<dynamic, dynamic> in constructor fails

I am new to Dart and trying to deserialize some json, but I can't quite figure out the trick.
I am able to cast the Map in main as shown, but the commented code does not work and I can't figure out why. I feel like there must be an easier way to do this (outside of using a package for code generation), but I'm not sure what that is.
import 'dart:convert';
import 'dart:io';
class Book {
final String title;
final String path;
Book(this.title, this.path);
Book.fromJson(Map<String, dynamic> content)
: title = content['title'],
path = content['path'];
Map<String, dynamic> toJson() =>
{
'title': title,
'path': path,
};
String toString() {
return '{ title: $title, path: $path }';
}
}
class Books {
final Map<String, Book> bookList;
Books(this.bookList);
// Books.fromJson(Map<String, dynamic> content)
// : bookList = Map<String, Book>.from(jsonDecode(jsonDecode(content)['books']).map((k,v) => MapEntry(k as String, Book.fromJson(v))));
Map<String, dynamic> toJson() =>
{
'books': jsonEncode(bookList),
};
String toString() {
return bookList.toString();
}
}
void main() {
Map<String, Book> bookList = {
"foo": Book("Foo", "/foo"),
"bar": Book("Bar", "/bar"),
};
Books books = Books(bookList);
print(books);
String content = jsonEncode(books);
print(content);
// print(Books.fromJson(jsonDecode(content)));
Map<String, Book> m = Map<String, Book>.from(jsonDecode(jsonDecode(content)['books']).map((k,v) => MapEntry(k as String, Book.fromJson(v))));
print(m);
}
Oops, I needed to remove an invocation of jsonDecode from Books.fromJson...
Books.fromJson(Map<String, dynamic> content)
: bookList = Map<String, Book>.from(jsonDecode((content)['books']).map((k,v) => MapEntry(k as String, Book.fromJson(v))));

Parse json array without names in Dart

I cannot parse such json
[{"operation_id":"38911","external_id":null,"status":"SUCCESS","date":"2019-12-01T12:30:08.000Z","amount":200}]
The problem lies in array with dynamic names. Here's my POJO:
class PaymentHistoryResponse {
final List<History> list;
PaymentHistoryResponse({this.list});
}
class History {
final String operationId;
final dynamic externalId;
final String status;
final DateTime date;
final int amount;
History({
#required this.operationId,
#required this.externalId,
#required this.status,
#required this.date,
#required this.amount
});
factory History.fromJson(String str) => History.fromMap(json.decode(str));
String toJson() => json.encode(toMap());
factory History.fromMap(Map<String, dynamic> json) => History(
operationId: json["operation_id"],
externalId: json["external_id"],
status: json["status"],
date: DateTime.parse(json["date"]),
amount: json["amount"]
);
Map<String, dynamic> toMap() => {
"operation_id": operationId,
"external_id": externalId,
"status": status,
"date": date.toIso8601String(),
"amount": amount
};
}
I also receive other json containing arrays, but named ones and I was able to decode them. How can I convert this one? P.s I've also made some research through this site and found some quite similar questions but a bit different and it didn't help me.
Since this is an array and not just a JSON you will need to do something like this:
mList = List<UserModel>.from(response.data.map((i) => UserModel.fromJson(i)));
Hint: for generating models with toJson and fromJson use this website:
https://javiercbk.github.io/json_to_dart/

Parse JSON to convert into Model class

I'm not able to convert server response to model class. The following is my code.
void main() {
//JSON to parse
var strJson = """{
\"person\": [
{\"name\": \"Mahendra\", \"age\": 28},
{\"name\": \"Test\", \"age\": 25}
]
}""";
var data = json.decode(strJson);
print("json: $data");
var result = PersonResponse<Person>.fromJSON(data);
print("result: ${result.persons}");
}
Model Class
class Person {
String name;
int age;
Person.fromJSON(Map json) {
this.name = json["name"];
this.age = json["age"];
}
}
class PersonResponse<T> {
List<T> persons;
PersonResponse.fromJSON(Map json) {
this.persons = json["person"];
}
}
When I run this code I'm not able to convert server response to model class. Getting following error...
Unhandled Exception: type List<dynamic> is not a subtype of type List<Person>
Whats wrong with my code. Any suggestions?
try
// To parse this JSON data, do
//
// final person = personFromJson(jsonString);
import 'dart:convert';
Person personFromJson(String str) => Person.fromJson(json.decode(str));
String personToJson(Person data) => json.encode(data.toJson());
class Person {
List<PersonElement> person;
Person({
this.person,
});
factory Person.fromJson(Map<String, dynamic> json) => Person(
person: List<PersonElement>.from(json["person"].map((x) => PersonElement.fromJson(x))),
);
Map<String, dynamic> toJson() => {
"person": List<dynamic>.from(person.map((x) => x.toJson())),
};
}
class PersonElement {
String name;
int age;
PersonElement({
this.name,
this.age,
});
factory PersonElement.fromJson(Map<String, dynamic> json) => PersonElement(
name: json["name"],
age: json["age"],
);
Map<String, dynamic> toJson() => {
"name": name,
"age": age,
};
}
I found 3 options:
You can either abandon usage of generics and replace List<T> persons; with List<dynamic> persons;. Your code will actually work with only this change.
You can abandon usage of generics and replace List<T> persons; with List<Person> and map it in fromJson.
class PersonResponse {
List<Person> persons;
PersonResponse.fromJSON(Map json) {
List<dynamic> list = json["person"];
persons = list.map((element) => Person.fromJSON(element)).toList();
}
}
Keep generics but restrict in to something serializable, to something like this:
class PersonResponse<T extends JsonSerializable> {
List<T> persons;
PersonResponse.fromJSON(Map json) {
List<dynamic> list = json["person"];
persons = list.map((element) => T.fromJSON(element)).toList();
}
}
Don't convert JSON into dart object manually. Because some time JSON response are very complex So it may happen you can write incorrect data type. So always use Online JSON to Dart Convertor. It is free to use. It will reduce the chance of error.