Home > Back-end >  Dart/Flutter json.decode and multiline value
Dart/Flutter json.decode and multiline value

Time:06-30

I have json data with multiline. When I use json.decode, I've a error.

"FormatException (FormatException: Control character in string 
... "count": "1", "price": "5", "description": "It is a long established fact

My Json Data

var str = {
"description": "It is a long established fact 
that a reader will be distracted by the readable content 
of a page when looking at its layout. The point 
of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English."
}

Thanks,

CodePudding user response:

Not sure if you want to encode or decode.

If you'd like to encode (create a JSON string out of data), you should make sure that the variable you supply to json.encode is of type Map<String, dynamic>. Also if you want to have multiline strings, you should use triple quotes """ for that in Dart. Here's an example

var data = {
"description": """It is a long established fact 
that a reader will be distracted by the readable content 
of a page when looking at its layout. The point 
of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English."""
};

final jsonString = json.encode(data);

If you want to decode (turning a JSON string into a Dart object), your input string should be formatted properly. JSON doesn't have support strings so you'll need to add line breaks \n, these then have to be ignored in your string declaration as well, just like the quotes within your JSON, resulting in \\n and \":

var str = "{\"description\": \"It is a long established fact\\nthat a reader will be distracted by the readable content\\nof a page when looking at its layout. The point\\nof using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English.\"}";

final data = json.decode(str);
  • Related