An open API service indexing awesome lists of open source software.

https://github.com/ztomz/json2dart


https://github.com/ztomz/json2dart

Last synced: 12 months ago
JSON representation

Awesome Lists containing this project

README

          

# Json2Dart

Json2Dart is a package to converts your map (json) to a String, wich is the output Dart code. You can write the String to a file or put it in a text widget.

## How to use

```Dart
import 'dart:convert';
import 'dart:io';

import 'package:json2dart/json2dart.dart';

void main(List args) {
// Your json String
String jsonString = '''
{
"weather": "sunny",
"clouds": "no",
"time": "11.30",
"sports you can do": [
"running",
"hiking",
"biking"
],
"any map": {
"key 1": "value 1",
"key 2": "value 2"
}
}''';

// Decode the json String to a Map
Map jsonData = jsonDecode(jsonString);

// Compile the json to Dart code
final String dartCode = Json2Dart.compileJson2Dart(jsonData, null);

// Output
print("Json:");
print(jsonData);
print("");
print("Dart:");
print(dartCode);

// Or write to a file
File dartCodeFile = File("${Directory.current.path}/example/generated_json.dart");
dartCodeFile.writeAsString(dartCode);
}
```