How can I extract a value from this simple JSON string in Excel VBA? -
i have simple json string in vba :
{ price_a: "0.172", price_b: "0.8", price_c: "1.3515" }
i extract value of price_a
0.172
.
how can done in excel vba? using microsoft office 2013.
i don't need extensive json library. simple function extract value of price_a
.
a regexp gives more control on parsing rather relying on character length.
as example:
sub test_function() dim strin string strin = "{" & _ "price_a: " & chr(34) & "0.182" & chr(34) & "," & _ "price_b: " & chr(34) & "0.8" & chr(34) & "," & _ "price_c: " & chr(34) & "1.3515" & chr(34) & "}" msgbox get_value(strin) end sub
extract first set of numbers in ""
after price a. can adapted check price b
public function get_value(strin string) string dim objregexp object dim objregmc object set objregexp = createobject("vbscript.regexp") objregexp .pattern = "price_a: ""([\d\.]+)""" if .test(strin) set objregmc = .execute(strin) get_value = objregmc(0).submatches(0) end if end end function
shorted version extract first set of numbers
public function get_value(strin string) string dim objregexp object set objregexp = createobject("vbscript.regexp") objregexp .pattern = "[\d\.]+" if .test(strin) get_value = .execute(strin)(0) end end function
Comments
Post a Comment