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

Discussion in 'Android Questions' started by bjfhs, Jul 9, 2018.

Tags:
  1. bjfhs

    bjfhs Active Member Licensed User

    I want to get the value "smoke":16
    the Json String
    Code:
    {
        
    "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":[]
    }
    Code:
    parsm.Initialize(strr)
        root=parsm.NextObject
        
    Dim id As String =root.Get("services")
    What can I do the next step?
     
    Last edited: Jul 9, 2018
  2. BillMeyer

    BillMeyer Well-Known Member Licensed User

    Erel and DonManfred like this.
  3. bjfhs

    bjfhs Active Member Licensed User

    Thank you ! But When I input the string in it ,and then click the button "Parse",It doesn't do anything.
     
  4. Sandman

    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.
     
    BillMeyer likes this.
  5. DonManfred

    DonManfred Expert Licensed User

    Looks like the json is not OK
     
  6. bjfhs

    bjfhs Active Member Licensed User

    Thank DonManfred and Andman!
    The next string is return from the web page,I did not delete any text.
    Code:
    {"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: Jul 9, 2018
  7. Sandman

    Sandman Well-Known Member Licensed User

    Yep, and that json parses just fine.
     
  8. DonManfred

    DonManfred Expert Licensed User

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

    Code:
    {    "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":[]
    }
    Code:
    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")
     
    BillMeyer likes this.
  9. Sandman

    Sandman Well-Known Member 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.
     
    BillMeyer likes this.
  10. bjfhs

    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.
     
  11. Sandman

    Sandman Well-Known Member Licensed User

    No changes required, the json is fine. Did you get it to work with http://basic4ppc.com:51042/json/index.html ?
     
  12. bjfhs

    bjfhs Active Member Licensed User

  13. Sandman

    Sandman Well-Known Member Licensed User

    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.
     
  14. Lahksman

    Lahksman Active Member Licensed User

    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 likes this.
  15. DonManfred

    DonManfred Expert Licensed User

    the website gives me an error on the position i saw a problem.

    Note that i tried to parse the json from post #1
     
  16. Sandman

    Sandman Well-Known Member Licensed User

    Yeah, the json in #1 was plain wrong. A corrected json (with nothing deleted) was posted in #6, so that's the one I commented.
     
  17. DonManfred

    DonManfred Expert Licensed User

    The json from #6 can be parsed without problem using http://basic4ppc.com:51042/json/index.html
     
  18. Sandman

    Sandman Well-Known Member Licensed User

    BillMeyer and DonManfred like this.
  19. bjfhs

    bjfhs Active Member Licensed User

  20. bjfhs

    bjfhs Active Member Licensed User

    Thank Sandman.
    My problem is solved, learning from your screenshot。
    Perhaps my brower has some problems.
     
    Sandman likes this.
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice