Android Question How can I get the value from Json string?

bjfhs

Active Member
Licensed User
I want to get the value "smoke":16
the Json String
B4X:
{
    "deviceId":"1b18c2bc-9f52-4f54-b52c-67c91551758c",
    "gatewayId":"1b18c2bc-9f52-4f54-b52c-67c91551758c",
    "nodeType":"GATEWAY",
    "createTime":"20180607T031240Z",
    "lastModifiedTime":"20180706T092418Z",

},
"services":[{
    "serviceId":"Smoke",
    "serviceType":"Smoke",
    "data":{
             "Smoke":16
           },
    "eventTime":"20180706T092418Z",
    "serviceInfo":null
}],

   "connectionInfo":{
       "protocolType":null
},
   "location":null,
   "devGroupIds":[]
}
B4X:
    parsm.Initialize(strr)
    root=parsm.NextObject
    Dim id As String =root.Get("services")
What can I do the next step?
 
Last edited:

Sandman

Well-Known Member
Licensed User
Also, it looks line you perhaps deleted a sensitive line from your json? That's fine, but doing so left a dangling comma in the lastModifiedTime line, which makes this json invalid.

Try removing that comma and then parse the json with the link from @BillMeyer - that'll work.
 

bjfhs

Active Member
Licensed User
Thank DonManfred and Andman!
The next string is return from the web page,I did not delete any text.
B4X:
   {"deviceId":"1b18c2bc-9f52-4f54-b52c-67c91551758c","gatewayId":"1b18c2bc-9f52-4f54-b52c-67c91551758c","nodeType":"GATEWAY","createTime":"20180607T031240Z","lastModifiedTime":"20180706T092418Z","deviceInfo":{"nodeId":"863703032164787","name":"test2","description":null,"manufacturerId":"FHS000","manufacturerName":"Fuhuasheng","mac":null,"location":"Shenzhen","deviceType":"Smoke","model":"FHS","swVersion":null,"fwVersion":null,"hwVersion":null,"protocolType":"CoAP","bridgeId":null,"status":"ONLINE","statusDetail":"NONE","mute":"FALSE","supportedSecurity":null,"isSecurity":null,"signalStrength":null,"sigVersion":null,"serialNumber":null,"batteryLevel":null},"services":[{"serviceId":"Smoke","serviceType":"Smoke","data":{"Smoke":16},"eventTime":"20180706T092418Z","serviceInfo":null}],"connectionInfo":{"protocolType":null},"location":null,"devGroupIds":[]}
 
Last edited:

DonManfred

Expert
Licensed User
The JSON have different problems. a , too much, a closing } too much...
Correct the json and the parser wil work

B4X:
{    "deviceId":"1b18c2bc-9f52-4f54-b52c-67c91551758c",    "gatewayId":"1b18c2bc-9f52-4f54-b52c-67c91551758c",    "nodeType":"GATEWAY",    "createTime":"20180607T031240Z",    "lastModifiedTime":"20180706T092418Z"},"services":[{
    "serviceId":"Smoke",
    "serviceType":"Smoke",
    "data":{
             "Smoke":16
           },
    "eventTime":"20180706T092418Z",
    "serviceInfo":null
}],

   "connectionInfo":{
       "protocolType":null
},
   "location":null,
   "devGroupIds":[]
}
B4X:
Dim parser As JSONParser
parser.Initialize(<text>)
Dim root As Map = parser.NextObject
Dim lastModifiedTime As String = root.Get("lastModifiedTime")
Dim createTime As String = root.Get("createTime")
Dim location As String = root.Get("location")
Dim services As List = root.Get("services")
For Each colservices As Map In services
 Dim serviceType As String = colservices.Get("serviceType")
 Dim data As Map = colservices.Get("data")
 Dim Smoke As Int = data.Get("Smoke")
 Dim eventTime As String = colservices.Get("eventTime")
 Dim serviceInfo As String = colservices.Get("serviceInfo")
 Dim serviceId As String = colservices.Get("serviceId")
Next
Dim nodeType As String = root.Get("nodeType")
Dim connectionInfo As Map = root.Get("connectionInfo")
Dim protocolType As String = connectionInfo.Get("protocolType")
Dim deviceId As String = root.Get("deviceId")
Dim devGroupIds As List = root.Get("devGroupIds")
Dim gatewayId As String = root.Get("gatewayId")
 

bjfhs

Active Member
Licensed User
Because the string is return from as web page ,and I can't change it,So I don't know how to do with it.
 

Sandman

Well-Known Member
Licensed User
No,it don't work.
I can't imagine why not. Your json is just fine, it should work without problems.

This is me taking your (newly edited) json from post #6 and using it in Erels parser at http://basic4ppc.com:51042/json/index.html. Parses just fine, as you can see.

upload_2018-7-9_11-19-13.png



And this is me taking the exact same json and running it through the validator at https://jsonlint.com/. Validates just fine.

upload_2018-7-9_11-21-0.png



I can't help you more unless you start providing concrete error messages. Also, run your json through these two services and post screenshots showing the errors.
 

Lahksman

Active Member
Licensed User
Thank DonManfred and Andman!
The next string is return from the web page,I did not delete any text.
B4X:
   {"deviceId":"1b18c2bc-9f52-4f54-b52c-67c91551758c","gatewayId":"1b18c2bc-9f52-4f54-b52c-67c91551758c","nodeType":"GATEWAY","createTime":"20180607T031240Z","lastModifiedTime":"20180706T092418Z","deviceInfo":{"nodeId":"863703032164787","name":"test2","description":null,"manufacturerId":"FHS000","manufacturerName":"Fuhuasheng","mac":null,"location":"Shenzhen","deviceType":"Smoke","model":"FHS","swVersion":null,"fwVersion":null,"hwVersion":null,"protocolType":"CoAP","bridgeId":null,"status":"ONLINE","statusDetail":"NONE","mute":"FALSE","supportedSecurity":null,"isSecurity":null,"signalStrength":null,"sigVersion":null,"serialNumber":null,"batteryLevel":null},"services":[{"serviceId":"Smoke","serviceType":"Smoke","data":{"Smoke":16},"eventTime":"20180706T092418Z","serviceInfo":null}],"connectionInfo":{"protocolType":null},"location":null,"devGroupIds":[]}
This json is not the same as the one you posted in your first post.
The one in the first post needs changes, the one in the qouted post is fine and should parse without problems.
 

DonManfred

Expert
Licensed User
I think you're wrong there. The json looks fine, as far as I can tell. I double checked with https://jsonlint.com/, which also says it's a valid json. Erels parser likes it also.
the website gives me an error on the position i saw a problem.

Error: Parse error on line 6:
...20180706T092418Z",},"services": [{ "
---------------------^
Expecting 'STRING', got '}'
Note that i tried to parse the json from post #1
 

bjfhs

Active Member
Licensed User
Thank Sandman.
My problem is solved, learning from your screenshot。
Perhaps my brower has some problems.
 
Top