0

Is there a way to parse a JSON as a Map<String, String> instead of Map<String, dynamic> when using Dart's json.decode.

For example with JSON of:

{
 'a': 2,
 'b': 'c'
}

It would parse into:

{
 'a': '2',
 'b': 'c'
}

1 Answer 1

1

Sadly, no. The code for decoding a Map starts with a Map<String, dynamic> and adds values as they are read, so there is no way to make the value type more specific.

Look into these for other options:

Sign up to request clarification or add additional context in comments.

1 Comment

Since parsing an unquoted 2 as a string would be a violation of the JSON specification, that's not something the JSON parse will do for you. It sounds like you need to convert the map. You can do: var jsonMap = jsonDecode(...); Map<String, String> myMap = Map.fromEntries(jsonMap.entries.map((e) => MapEntry(e.key, "${e.value}")));.

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.