1

I want to convert nested json string into json how could i do this

{2022: {Apr: 100.0}} // this is my string I/flutter ( 7168): Error FormatException: Unexpected character (at character 2)// here error comes out I/flutter ( 7168): {2022: {Apr: 100.0}}

jsonDecoder("jsonstring here")// I used this code

3 Answers 3

1

First encode String then decode it

import 'dart:convert';

void main() {
 String i = "{2022: {Apr: 100.0}}";
 var i2 =  json.decode(json.encode(i));
  print(i2);
}
Sign up to request clarification or add additional context in comments.

Comments

0

You can import the dart library 'dart:convert' and use its method json.decode(<your json string>) to convert a JSON string into a JSON.

2 Comments

No it is giving error
Error FormatException: Unexpected character (at character 2)
0

Not exactly sure about the question, but to encode everything one has these options:

One needs to add the flag before executing build_runner.
https://docs.flutter.dev/data-and-backend/serialization/json#generating-code-for-nested-classes

@JsonSerializable(explicitToJson: true)
class User {}

Or update/create the build.yaml in your root.
https://github.com/rrousselGit/freezed/issues/86#issuecomment-593936459

targets:
  $default:
    builders:
      json_serializable:
        options:
          explicit_to_json: true

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.