locked
Newtonsoft: Access particular value RRS feed

  • Question

  • User940894612 posted

    Hi

    I was wondering if someone could help me access a particular property value from a json string that I've run a query on.  

    Json response:  

    loginResponse: {
    resultCode: 0,
    session: [
    {
    sessionToken: "F8816FEB3D19F216923081A9805C99A6",
    svcGroupName: "ABCDEF",
    svcGroupIdentifier: "861DC9BD7F4E7DD3CCCD534D0AE2A2E9"
    },
    {
    sessionToken: "2453C9F6E087E0B0E23E3EE9F8602224",
    svcGroupName: "40009635-60",
    svcGroupIdentifier: "DB5CEA26CA37AA09E5365F3E7F5DD9EB"
    }
    ]
    }

    I would like the get the sessionToken value where svcGroupName is "4009635-60"

    So far I've done the following:

    string url = "www.xxx.com";            
    var request = WebRequest.Create(url);
    
                request.Method = "GET";
                request.ContentType = "application/json; charset=utf-8";
    
                WebResponse response = request.GetResponse();
                Stream webStream = response.GetResponseStream();
                StreamReader responseReader = new StreamReader(webStream);
                var response1 = responseReader.ReadToEnd();
                responseReader.Close();
                JObject o = JObject.Parse(response1);
                string ControlGroup = (string)o["loginResponse"]["session"][2]["sessionToken"];
                Response.Write(ControlGroup);
                JToken acme = o.SelectToken("loginResponse.session[?(@.svcGroupName == '4009635')]");
                Response.Write((acme));

    I get the response:  { "sessionToken": "1CC55BFDE0E5B58159568FE595BB54F9", "svcGroupName": "4009635", "svcGroupIdentifier": "52569C045DC348F12DFC4C85000AD832" } - but would just like the value inside sessionToken.

    Any idea on how to achieve this. 

    Thanks

    Rob

    Wednesday, October 18, 2017 2:58 PM

All replies

  • User2103319870 posted

    You can use  Newtonsoft Json Linq to fetch the results from your json file

     string response1 = "{resultCode: 0,session: [{ sessionToken: 'F8816FEB3D19F216923081A9805C99A6',svcGroupName: 'ABCDEF',svcGroupIdentifier: '861DC9BD7F4E7DD3CCCD534D0AE2A2E9'},{ sessionToken: '2453C9F6E087E0B0E23E3EE9F8602224',svcGroupName: '40009635-60',svcGroupIdentifier: 'DB5CEA26CA37AA09E5365F3E7F5DD9EB'}]}            ";
                    JObject o = JObject.Parse(response1);
                    //Fetch all sessiontoken values
                    var result = o.SelectToken("session").Select(i => new { sessionToken = i.SelectToken("sessionToken").ToString() }).ToList();
                    //loop through results
                    foreach (var obj in result)
                    {
                        //Get the value here
                        string token = obj.sessionToken.ToString();
                    }

    Wednesday, October 18, 2017 8:46 PM
  • User-1838255255 posted

    Hi Robbied81,

    According to your description, about how to get the svcGroupName value from the above JSON Data, please check the following sample code:

    Sample Code:

    <head runat="server">
        <title></title>
        <script>
            var loginResponse = {
                resultCode: 0,
                session: [{
                    sessionToken: "F8816FEB3D19F216923081A9805C99A6",
                    svcGroupName: "ABCDEF",
                    svcGroupIdentifier: "861DC9BD7F4E7DD3CCCD534D0AE2A2E9"
                },
                {
                    sessionToken: "2453C9F6E087E0B0E23E3EE9F8602224",
                    svcGroupName: "40009635-60",
                    svcGroupIdentifier: "DB5CEA26CA37AA09E5365F3E7F5DD9EB"
                }
                ]
            };
            document.write(loginResponse.session[1].svcGroupName);
        </script>
    </head>

    Best Regards,

    Eric Du

    Thursday, October 19, 2017 3:12 AM