blackrainbow Posted July 20, 2015 Share Posted July 20, 2015 (edited) Hello, I have a very long json string and I want to transform it to variables. The json look like this:{"Brayan":{"id":53192745,"name":"Brayan","profileIconId":636,"age":30,"something":1437402511000}}I make a short version but it's always designed like this:"name":"Value",and I would like to parse it and get it into variables, so my above exemple become:$name = "Value"I get this json code from a get request inside my code, so I can't edit it on a program, I have to do it in AutoIt.I don't really understand JSON and I see this post : https://www.autoitscript.com/forum/topic/148114-a-non-strict-json-udf-jsmn/But I don't rlly understand how works this part of code (because I don't understand JSON):Local $Obj Json_Put($Obj, ".foo", "foo") Json_Put($Obj, ".bar[0]", "bar") Json_Put($Obj, ".test[1].foo.bar[2].foo.bar", "Test") Local $Test = Json_Get($Obj, '["test"][1]["foo"]["bar"][2]["foo"]["bar"]') ; "Test"I think it would be useful for what I want to do.I can't do something like this:$json_string = '{"Brayan":{"id":53192745,"name":"Brayan","profileIconId":636,"age":30,"something":1437402511000}}' Global $parsed_json_string = StringSplit($json_string, '"') $string = StringReplace($parsed_json_string[5], ":", ""); ' :53192745, ' -> string (without quotes) $parsed_json_string[5] = StringReplace($string, ",", ""); ' 53192745, ' -> string (without quotes) $id = $parsed_json_string[5]Because my json string is much too big for coding this on every variables I want.My idea is to get a function that can do this:#include <MsgBoxConstants.au3> $brayan = '{"Brayan":{"id":53192745, "age":30}}' $x = FunctionOfMyDreams($brayan, "age") MsgBox($MB_OK, "", $x); -> will return "30"Hope someone know how to do this, it would be very usefull for my project Edited July 20, 2015 by blackrainbow Link to comment Share on other sites More sharing options...
mikell Posted July 21, 2015 Share Posted July 21, 2015 ?#Include <Array.au3> $str = '{"Brayan":{"id":53192745,"name":"Brayan","profileIconId":636,"age":30,"something":1437402511000}}' $str = StringReplace($str, '"', "") $res = StringRegExp($str, '(?:id|name|age):([^\{,}]+)', 3) _ArrayDisplay($res) Link to comment Share on other sites More sharing options...
SadBunny Posted July 21, 2015 Share Posted July 21, 2015 Or, using that UDF you pointed to and it's machine code parser:#include "Json.au3" $jsonString = '{"Brayan":{"id":53192745,"name":"Brayan","profileIconId":636,"age":30,"something":1437402511000}}' $Obj = Json_Decode($jsonString) $age = functionOfMyDreams($Obj, "Brayan", "age") ConsoleWrite("Age: " & $age & @CRLF) Exit Func functionOfMyDreams($decodedObject, $targetObject, $targetKeyInObject) $value = Json_Get($decodedObject, '["' & $targetObject & '"]["' & $targetKeyInObject & '"]') return $value EndFunc ;==>functionOfMyDreams blackrainbow and mLipok 2 Roses are FF0000, violets are 0000FF... All my base are belong to you. Link to comment Share on other sites More sharing options...
blackrainbow Posted July 21, 2015 Author Share Posted July 21, 2015 Thanks, I'll try this and I tell you if it works Link to comment Share on other sites More sharing options...
blackrainbow Posted July 21, 2015 Author Share Posted July 21, 2015 Ok, I test both, both works but this methode:Or, using that UDF you pointed to and it's machine code parser:#include "Json.au3" $jsonString = '{"Brayan":{"id":53192745,"name":"Brayan","profileIconId":636,"age":30,"something":1437402511000}}' $Obj = Json_Decode($jsonString) $age = functionOfMyDreams($Obj, "Brayan", "age") ConsoleWrite("Age: " & $age & @CRLF) Exit Func functionOfMyDreams($decodedObject, $targetObject, $targetKeyInObject) $value = Json_Get($decodedObject, '["' & $targetObject & '"]["' & $targetKeyInObject & '"]') return $value EndFunc ;==>functionOfMyDreams Don't work if there is multiples times the same variables name but with differerants values, for exemple:$jsonString = '{"Brayan":{"id":53192745,"name":"Brayan","profileIconId":636,"age":30,"something":1437402511000,"something":18975}}'it will just return 18975, it would be great if it can return a table like:$value[0] = 1437402511000 $value[1] = 18975 Link to comment Share on other sites More sharing options...
SadBunny Posted July 21, 2015 Share Posted July 21, 2015 rfc7159 states that the keys should be unique. Not that json is syntactically incorrect if that isn't the case, but A.F.A.I.K. Json parsers normally just return the last matching key. But you didn't ask about that If you want multiple elements in an array, you can specify the element count with ' ["array_name"][<element index>] '. I believe there's also a function to get the object count. Haven't looked into it that far. But yeah, if you know what you need specifically and you'd prefer automatic array creation and less code over speed and pre-baked parsing code then regex is certainly an option as well, of course Roses are FF0000, violets are 0000FF... All my base are belong to you. Link to comment Share on other sites More sharing options...
mikell Posted July 21, 2015 Share Posted July 21, 2015 In case of 2 variables with same name the 2nd value overwrites the 1st one... strange syntaxBut regex doesn't care about the syntax accuracy #Include <Array.au3> $str = '{"Brayan":{"id":53192745,"name":"Brayan","profileIconId":636,"age":30,"something":1437402511000,"something":18975}}' $str = StringReplace($str, '"', "") $res = StringRegExp($str, '(?:id|name|age|something):([^\{,}]+)', 3) _ArrayDisplay($res) Jasp402 1 Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now