Search code examples
stringapilua

Need to get a Specified word or value from a response string


I am trying to get a value from a third party device response.

the response is as follows;

{"p_targetId":0,"p_cmd":"getProductListSaved","p_rspStatus":"SUCCESS","p_msg":"","p_data":[{"globalproductlistsavedID":"1","ProductCustomName":"GDC","ProductIp":"192.168.168.","ProductMask":"255.255.255.0","ProductIsDhcp":"0","ProductIpTwo":"192.168.168.","ProductMaskTwo":"255.255.255.0","ProductIsDhcpTwo":"0","ProductSupported_ProductSupportedID":"3","ProductLogin":"","ProductPassword":"","ProductIdOverride":"1"}]}

In my lua script am writing, with data representing the above response;

name, value = string.match(data,"(.+):(.+)") Controls.DeviceName.String = tostring(value)

from this am always getting; "1"}]}

Is it possible to specify which part of the response I want to extract? example: "GDC" or "192.168.168.**"


Solution

  • Thats a JSON String.
    So the best/fastest way is JSON support for Lua.
    The Linux Editor NeoVIM (nvim) has LuaJIT and JSON support.
    And that can make things so easy...
    NeoVIM run Lua to decode a JSON String ( Also available for Windows, macOS and Android (With Termux) )
    Visit: https://github.com/neovim/neovim