-->
Page 2 of 2

Re: JSON Parser

PostPosted: Tue Jan 13, 2015 3:38 am
by kmpm
a) I think nodeMCU is very much a part of this forum and it would be quite relevant to ask questions about it. Just look at the name of the forum section "Platforms -> LUA nodeMCU.com".[/list=]

b) Doing this in C (if you are now running in nodeMcu) would be a good thing since I think this would be a
thing that a lot of people are going to use for a lot of things[/*]

Re: JSON Parser

PostPosted: Thu Mar 05, 2015 8:15 pm
by Fab85
It would be nice to do some json parsing.

Re: JSON Parser

PostPosted: Thu Mar 05, 2015 9:51 pm
by Fab85
I made some small changes to Bycocojackr42 script. It's still quike and dirty .

Code: Select allfunction fromJson(MSG)
   local hashMap = {}
   local i = 1
   local j = 1
   
   for val in string.gmatch(MSG,'"%w+":"%w+",') do
      hashMap[i] = {}
      val = string.gsub(val, '":"', " ")
      val = string.gsub(val, '"', "")
      val = string.gsub(val, ',', "")
     
      for sval in string.gmatch(val, "%S+") do         
          hashMap[i][j] = sval;
          j = j + 1
      end
      j = 1
      i = i+1
     
   end
   
   for val in string.gmatch(MSG,'"%w+":"%w+"}') do
      hashMap[i] = {}
      val = string.gsub(val, '":"', " ")
      val = string.gsub(val, '"', "")
      val = string.gsub(val, '}', "")
     
      for sval in string.gmatch(val, "%S+") do       
          hashMap[i][j] = sval;
          j = j + 1
      end
      j = 1
      i = i+1
   end
   return hashMap
end

function toJson(MAP)
   local outMsg = '{'
   local i = 1
   
   repeat
      outMsg = outMsg..'"'..map[i][1]..'":"'..map[i][2]..'",'
      i = i + 1
   until map[i] == nil
   
   return string.sub(outMsg,0,-2)..'}'
end

Re: JSON Parser

PostPosted: Thu Apr 02, 2015 5:29 am
by Michael Lee
This is supported in the latest firmware:

Code: Select alllocal json = require "cjson"
luaObject = json.decode("{\"json\": \"string\"}");
jsonString = json.encode(luaObject)