JSON decode - lost information

조회 수: 12 (최근 30일)
Simon Parten
Simon Parten 2018년 12월 12일
댓글: Wendell Wilkerson 2021년 9월 10일
Let's say I get this string from the serverside;
{"1D2381B":["fd56c0fd-8882-4b8e-9941-0bd04489981e"],"BZ5ZZ65":["2c768773-7d5f-4d06-8fa1-6ce819dc383a"],"11852420":["b8953809-a264-41b3-a1c3-f3592cd51419"]}
I want to be able to parse and use this information. Matlab automagically decodes it... but it 'makes valid names' of my IDs.
As modifying an ID somewhat destroys it's utility. I'd like a way to prevent or otherwise recover the information. The function 'makeValidNames' returns a second argument, which says which entries were modified.
This information is lost in the JSON decode function. Can anyone help with a workaround, on how I can convert this without the destruction of information?

채택된 답변

Guillaume
Guillaume 2018년 12월 12일
It's something I've complained about to Mathworks in the early days of jsondecode. In fact, you can use matlab jsonencode to encode a matlab.containers.Map that you would not be able to read back correctly with jsondecode due to that mangling of json keys.
I suggested that jsondecode returns a matlab.containers.Map instead which wouldn't require name mangling and the developer did say that it was something they were considering for a future release. Either they have decided against it or it's yet to be implemented.
I suggest that you raise a service request to Mathworks. The more people complain, the more likely it will change.
In the meantime, I'm afraid you'll have to delegate to a 3rd party parser or write your own. It's not that difficult to parse json actually.
  댓글 수: 4
Guillaume
Guillaume 2018년 12월 12일
You can simplify this slightly by replacing:
raw = fread(fid,inf);
str = char(raw');
by
str = fread(fid, [1 Inf], '*char');
Wendell Wilkerson
Wendell Wilkerson 2021년 9월 10일
+1 Support ticket submitted. I had the same thought about using containers.Map instead of structure to avoid mangling names.
In the mean time, I modified the parse_json function available on the file exchange (https://www.mathworks.com/matlabcentral/fileexchange/20565-json-parser) to use containers.Map instead of structures. It worked for my meager needs.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

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

제품


릴리스

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by