首页 > 解决方案 > 在 mql4 中处理 json 字符串

问题描述

我收到了以下字符串:

{"records":[{"id":"rec4haaOncoQniu8U","fields":{"orders1":5},"createdTime":"2020-02-08T09:08:22.000Z"}]}

我不明白如何使用位于此处的“JAson.mqh”库处理和分离 mql4 中 json 的值:https ://www.mql5.com/en/code/13663

我需要位于 "fields" 下的 "orders" 的值,value = 5。唯一改变的“键”是“字段”值中的键。

我希望能够通过以下方式获取值:

string value1 = Result[0].["fields"].["orders1"]; //5
string value2 = Result[0].["fields"].["orders2"];

请让我知道我能做什么。

标签: mql4metatrader4mql5

解决方案


您可以使用以下格式获取值。请注意,它必须强制转换为类型。(我已将其转换int为 中的类型JSON,但您也可以将其转换string为)

int value1 = json["records"][0]["fields"]["orders1"].ToInt(); // if you want to make it a string use ToStr() instead of ToInt()

这是我所做的完整示例

string jsonString = "{\"records\": [{\"id\": \"rec4haaOncoQniu8U\",\"fields\": {\"orders1\": 5 }\"createdTime\": \"2020-02-08T09:08:22.000Z\"}]}";

if(json.Deserialize(jsonString))
   Alert(json["records"][0]["fields"]["orders1"].ToInt());

希望它有所帮助。


推荐阅读