Processing json string in mql4

跟風遠走 提交于 2020-04-17 18:46:11

问题


I have received the following string:

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

I am not understanding how I can process and separate the values of the json in mql4 using the "JAson.mqh " library, located here: https://www.mql5.com/en/code/13663

I need the values of "orders" located under "fields" , value = 5. the only "KEYS" that changes are the keys within the "fields" values.

i would like to be able to get the values with something like this:

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

Please let me know what I can do.


回答1:


You can get the value using the following format. Note that it has to be casted to a type. (I have casted it to int as it is the type it is in the JSON, but you can cast it to string as well)

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

Here is a full example of what I did

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());

Hope it helped.



来源:https://stackoverflow.com/questions/60801625/processing-json-string-in-mql4

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!