필터 지우기
필터 지우기

Critical error in jsonencode....?

조회 수: 6 (최근 30일)
Seb
Seb 2017년 7월 10일
답변: Pratik Anand 2017년 7월 11일
So, I think I have found an error in the way matlab's built in jsonencode() function works. If you create a struct with one field of doubles, and a double has a infinity value (inf or -inf) jsonencode will encode this without quotation ("") marks. Therefore if I put this through a JSON validator (https://jsonlint.com/) it fails. I am using JSONLab toolbox in the meantime, however it would be nice to work with the native matlab function. Here is the code for an example:
x(1).a = 'hello'
x(1).b = 12
x(2).a = 'bye'
x(2).b = -inf
example = jsonencode(x)
Which will then output:
[{"a":"hello","b":12},{"a":"bye","b":-Inf}]
Where infact the correct output should be:
[{"a":"hello","b":12},{"a":"bye","b":"-Inf"}]

채택된 답변

Pratik Anand
Pratik Anand 2017년 7월 11일
It is a documented behavior with MATLAB JSONLab toolbox till version 2017a. With version 2017b, Inf and NaN values will be converted to null as per ECMAScript JSON specification. Thus, you can wait and upgrade to 2017b when it comes out.
Alternatively, I can suggest that you replace -Inf in your code to null manually before json encoding it.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 JSON Format에 대해 자세히 알아보기

제품

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by