JSON decode - lost information
조회 수: 14 (최근 30일)
이전 댓글 표시
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?
댓글 수: 0
채택된 답변
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
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
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 Center 및 File Exchange에서 JSON Format에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!