使用Matlab读取JSON对象 [英] Read JSON objects with Matlab
问题描述
我想使用Matlab从json文件中读取内容,并将所有内容作为对象存储在数据"中.导入后,如果对象中可用,我需要遍历所有对象并提取特定值.
I want to read from a json file with Matlab and store everything in "data" as objects. After import, I need to iterate through all and extract specific values, if it's available in the object.
JSON(源):
{
"eid": 44000,
"dpm_id": {
"dpm": "fm",
"pwr": "main"
},
"fpga_id": 3189637128,
"fpga_ver": 3104379702,
"boot_id": 0,
"pbs_ver": "PBS 2012-05-07 16:41"
}
{
"sid": 1,
"hk1": {
"bela_mode": "pbs_mode",
"pbs_version": "version 1.3",
"scet": "2038-01-19T03:14:08",
"ref_time": "0:00:00",
"tc_received": 2,
"tc_exec": 2,
"tc_err_ack": 0,
"tc_err_exec": 1,
"tm_total": 1,
"tm_sent": 1,
"tm_dropped": 0,
....
Matlab (import, according to this website, resp. Class):
fname = 'FileName.json';
fid = fopen(fname);
raw = fread(fid,inf);
str = char(raw');
fclose(fid);
data = JSON.parse(str)
问题/问题:
如您所见,Matlab仅读取第一个方括号/字段的内容.即使我不知道有多少个括号/字段,我该如何导入?
As you see, Matlab only reads the content of the first brackets/field. How can I import ALL brackets/fields, even if I don't know how many there are?
data =
eid: 44000
dpm_id: [1x1 struct]
fpga_id: 3.1896e+09
fpga_ver: 3.1044e+09
boot_id: 0
pbs_ver: 'PBS 2012-05-07 16:41'
谢谢!
推荐答案
您正在尝试读取无效的json文件.我建议使用 jsonlint 进行快速验证.
You are trying to read a json file, which is not valid. I recommend to use jsonlint for a quick verification.
您的json看起来像
{
"skipped":"A"
}
{
"skipped":"B"
}
这不是有效的语法,因为它描述了两个对象.在第一个}
之后,解析器期望文件结尾,因为json文件包含一个对象.
That is not a valid syntax, because it describes two objects. After the first }
the parser expects the end of file because a json file contains one object.
可能的解决方法是:
[
{
"skipped": "A"
},
{
"skipped": "B"
}
]
或
{
"aa": {
"skipped": "A"
},
"bb": {
"skipped": "B"
}
}
这篇关于使用Matlab读取JSON对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!