Boost库解析JSON文件用法总结

JSON语法浅析

JSON 语法是 JavaScript 语法的子集。

  • 数据在名称/值对中
  • 数据由逗号分隔
  • 花括号保存对象
  • 方括号保存数组

JSON值

  • 数字(整数或浮点数)
  • 字符串(在双引号中)
  • 逻辑值(true 或 false)
  • 数组(在方括号中)
  • 对象(在花括号中)
  • null

JSON对象

对象可以包含多个名称/值对

{ "firstName":"John" , "lastName":"Doe" }

JSON数组

数组可包含多个对象

{
"employees": [
{ "firstName":"John" , "lastName":"Doe" },
{ "firstName":"Anna" , "lastName":"Smith" },
{ "firstName":"Peter" , "lastName":"Jones" }
]
}

Boost库解析JSON文件

{
   “Version”: 1,
	"Metrics": [{
			"wingarea": 1341.0,
			"unit": "FT2"
		}, {
			"wingspan": 1350.81,
			"unit": "Inch"
		}
	],

	"Atmosphere": [{
			"TableName": "atmostemp",
			"Dimension": 2
		}
	],

	"Propulsion": {
		"Location": {
			"XEDIC": 22.618,
			"ZEDIC": 4.091,
			"METIC": 3.829,
			"YETIC": 16.14,
			"unit": "FT"
		},
		"Orient": {
			"EXZT_FT": 1.14,
			"EXZT_RT": 181.17,
			"EXYT_FT": 1.14,
			"EXYT_RT": -1.14,
			"unit": "DEG"
		}
	},

	"MassProperties": {
		"OEW": [{
				"Ixx": 6725.7e+06,
				"unit": "lb-in.sq"
			}, {
				"Iyy": 56003.0e+06,
				"unit": "lb-in.sq"
			}
		],
		"DataTable": [{
				"TableName": "FlapIxx",
				"Dimension": 2
			}, {
				"TableName": "FlapIyy",
				"Dimension": 2
			}3
			}
		],
		"CenterFuelTank": {
			"Location": {
				"X": 604.9,
				"Y": 0.0,
				"Z": 174.5,
				"Unit": "inch"
			},
			"Fuel": {
				"Capacity": 28730,
				"Weight": 9000,
				"Unit": "lbs"
			},
			"DataTable": [{
					"TableName": "CFTX",
					"Dimension": 3
				}, {
					"TableName": "CFTZ",
					"Dimension": 3
				}
			]
		},
		"LeftFuelTank": {
			"Location": {
				"X": 688.7,
				"Y": -249.4,
				"Z": 211.6,
				"Unit": "inch"
			},
			"Fuel": {
				"Capacity": 8677,
				"Weight": 7500,
				"Unit": "lbs"
			},
			"DataTable": [{
					"TableName": "WFTX",
					"Dimension": 3
				}, {
					"TableName": "WLFTY",
					"Dimension": 3
				}
			]
		}
	},

	"Aerodynamics": {
		"Others": [{
				"TableName": "dfef_2_1_1",
				"Dimension": 2
			}, {
				"TableName": "alreat_2_10_1",
				"Dimension": 2
			}
		],
		"LiftForce": [{
				"TableName": "clbnas_3_1_1",
				"Dimension": 3
			}, {
				"TableName": "clbas_3_1_2",
				"Dimension": 3
			}
		],
		"DragForce": [{
				"TableName": "cdbnas_4_1_1",
				"Dimension": 3
			}, {
				"TableName": "cdbas_4_1_2",
				"Dimension": 3
			}
		],
		"SideForce": [{
				"TableName": "kyh_8_1_4",
				"Dimension": 2
			}, {
				"TableName": "kybe_8_1_6",
				"Dimension": 3
			}
		],
		"PitchMoment": [{
				"TableName": "cmbnas_5_1_1",
				"Dimension": 3
			}, {
				"TableName": "cmbas_5_1_2",
				"Dimension": 3
			}
		],
		"RollMoment": [{
				"TableName": "dcrssge_6_1_3",
				"Dimension": 4
			}, {
				"TableName": "krh_6_1_4",
				"Dimension": 2
			}
		],
		"YawMoment": [{
				"TableName": "dcnssge_7_1_3",
				"Dimension": 4
			}, {
				"TableName": "knh_7_1_4",
				"Dimension": 2
			}
		]
	}
}

JSON文件加载方法

// 创建ptree对象
boost::property_tree::ptree json_root;
// 读取file文件,并将根节点存储赋值给json_root
boost::property_tree::read_json<boost::property_tree::ptree>(file_name, json_root); 

键值对获取方法

json_root.get<int>("Version");

获取子节点的方法

boost::property_tree::ptree elements = json_root.get_child("MassProperties");

对象中包含对象的解析方法

"Propulsion": {
		"Location": {
			"XEDIC": 22.618,
			"ZEDIC": 4.091,
			"METIC": 3.829,
			"YETIC": 16.14,
			"unit": "FT"
		},
		"Orient": {
			"EXZT_FT": 1.14,
			"EXZT_RT": 181.17,
			"EXYT_FT": 1.14,
			"EXYT_RT": -1.14,
			"unit": "DEG"
		}
	}
  • 先用get_child获取子对象Location
  • 再用获取键/值对的方法获取值

代码如下所示

boost::property_tree::ptree ptree_location = prop_element.get_child("Location");
ThrustVector.XEDIC = ptree_location.get<double>("XEDIC");

数组中包含对象的解析方法

Metrics": [{
			"wingarea": 1341.0,
			"unit": "FT2"
		}, {
			"wingspan": 1350.81,
			"unit": "Inch"
		}, {
			"mac": 155.81,
			"unit": "Inch"
		}
]
  • 首先通过迭代器获取首地址,类型为boost::property_tree::ptree::value_type*

  • value_type实际是std::pair<const Key, self_type>

      boost::property_tree::ptree::iterator json_iterator = metric_elements.begin();
      m_wing_area = json_iterator->second.get<double>("wingarea"); json_iterator++;
      m_wing_span = json_iterator->second.get<double>("wingspan"); json_iterator++;
    

配合boost::optional实现有效值的判断

 boost::optional<const boost::property_tree::ptree&> ptree_location =
            prop_element.get_child_optional("Location");

if(ptree_location) {
    ThrustVector.XEDIC = ptree_location->get<double>("XEDIC");
    ThrustVector.ZEDIC = ptree_location->get<double>("ZEDIC");
    ThrustVector.METIC = ptree_location->get<double>("METIC");
    ThrustVector.YETIC = ptree_location->get<double>("YETIC");
}

版权声明:本文为jinzhu1911原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。