text
stringlengths
1
330k
>> for n = 1:f.length, fa(n)=f.get(n-1); end
>> fa
fa =
1 2 3
Json.NET and fastJSON
These libraries require you to add add the path of the .NET dll to Matlab using NET.addAssembly. For fastJSON, since binaries are not available, you will also need to compile the .NET dll. I’ll leave it to the reader to investigate further, independently.
Categories: Java, Low risk of breaking in future versions
Tags: ,
Bookmark and SharePrint Print
18 Responses to JSON-Matlab integration
1. FYI: scipy.io.loadmat can read a MAT file into Python.
2. Rune Juhl Jacobsen says:
There’s also https://github.com/christianpanton/matlab-json . While I haven’t benchmarked it, it should be a lot faster than Matlab-only implementations.
3. Pingback: Working with JSON & Matlab | Alejandro Clemotte
4. I wish I had put more detail into using Newtonsoft JSON.net. Here’s a Gist demo
% download JSON.net from https://github.com/JamesNK/Newtonsoft.Json/releases
% extract into MATLAB folder
% ====
% add assembly (assuming not registered in GAC)
asm = NET.addAssembly('%HOME%\Documents\MATLAB\Json70r1\bin\Net35\Newtonsoft.Json.dll')
% using the following example:
% {"inputs":
% {"module":
% {"name": "solarpanel-435",
% "nameplate": 435
% }
% },
% {"request":
% {"startdate": [2015,1,1,1,0,0],
% "enddate": [2015,12,31,23,0,0]
% }
% }
% }
% get source using io
str_data = fileread('path/to/file.json')
% use Json.Convrters.DeserializeObject(str_data)
json_data = Newtonsoft.Json.JsonConvert.DeserializeObject(str_data)
% getting value by key
inputs = json_data.GetValue('inputs') % get keys
module = inputs.GetValue('module')
% getting value of Newtonsoft.Json.Linq.JValue
name = module.GetValue('name').Value() % return value of name
nameplate = module.GetValue('nameplate').Value()
% getting items in JArray
startdate(1) = inputs.GetValue('request').Item(0)
startdate(2) = inputs.GetValue('request').Item(1)
startdate(3) = inputs.GetValue('request').Item(2)
startdate(4) = inputs.GetValue('request').Item(3)
startdate(5) = inputs.GetValue('request').Item(4)
startdate(6) = inputs.GetValue('request').Item(5)
% prettyprint output
% create a LINQ JObject
json_obj = Newtonsoft.Json.Linq.JObject() % create object
% view methods
methodsview(json_obj) % view all methods for JObject
% add values, objects and arrays
json_obj.Add('this', Newtonsoft.Json.Linq.JValue('that'))
json_obj.Add('foo', Newtonsoft.Json.Linq.JArray([10,20,30]))
json_obj.Add('bar', Newtonsoft.Json.Linq.JArray({'x','y','z'}))
% prettyprint
json_obj.ToString() % pretty print
% serialize json object
json_str = json_obj.Add('foo', Newtonsoft.Json.Linq.JArray([10,20,30]))
fid = fopen('outputfile.json')
5. آموزش متلب says:
Very Nice
6. ET says:
I think these are semi undocumented in the latest Matlab versions (i.e. R2015a or R2015b) but they are much faster then most other solutions:
jsonString = matlab.internal.webservices.toJSON(s);
• Brad Stiritz says:
ET, I tried your code without success in R2015a :
>> matlab.internal.webservices.toJSON('abc')
Undefined variable "matlab" or class "matlab.internal.webservices.toJSON".
• Yair Altman says:
@Brad – toJSON() and fromJSON() were added in R2015b
7. oro77 says:
I am using the following command to create the JSON string.
jsonString = matlab.internal.webservices.toJSON(s);
It works but if my structure is using a real like below.
s = struct('this', {'a', 'b'}, 'that', {0.1, 2})
jsonString will return
Why is it 0.10000000000000001 instead of 0.1 ?
How can I correct this ?
• Malcolm Lidierth says:
For a long answer:
What Every Computer Scientist Should Know About Floating-Point Arithmetic, by David Goldberg, published in the March, 1991 issue of Computing Surveys. Copyright 1991, Association for Computing Machinery, Inc.
Short answer:
Just as 1/3 can not be exactly represented in decimal, 1/10 has no exact representation in binary.
The java.lang.Double class is probably underused by MATLABers: its toString() method returns a string representation of a double that uses the minimum number of digits required to exactly reproduce that double during deserialisation (i.e to get back to that member of the subset of numbers represented in IEEE double).
java.lang.Double uses the FloatingDecimal class
• oro77 says:
Thank you for your reply.
So the best way to keep 0.1 would be to store in my structure a string instead of a double value.
• @oro77 – I would think that it would make more sense to simply round off the insignificant digits upon loading/displaying (in those rare cases where this difference in LSB makes any difference), rather than store string representations in the JSON payload.
• oro77 says:
@Yair Altman – Thank you for your advice. I was thinking about rounding off it but the user may want to edit the json file in a text editor and he may find awkward about the insignificant digits. It is hard to decide what is the best solution. Is there something wrong about storing string representations in the JSON payload ?
• Malcolm Lidierth says:
The number of digits used depends on the JSon library and how it does the formatting. Sun/Oracle/openJDK provide a full spec for the java.lang.Double.toString() method. Remembering that MATLAB calls that method to get the text to display in the MATLAB Command Window for Java objects including Double, the following may be informative:
>> a = java.lang.Double('0.1')
a =