How to get JSON String value?

var responseFromServer =
  // lines split for readability
  "{\"flag\":true,\"message\":\"\",\"result\":{\"ServicePermission\":true,"
  +  "\"UserGroupPermission\":true}}";
var serializer = new System.Web.Script.Serialization.JavaScriptSerializer();
var responseValue = serializer.DeserializeObject(responseFromServer);

responseFromServer value gets webservice, and then how to get JSON string value, such as "flag", "Servicepermission"

affix: sorry using C # for this.

+3
source share
2 answers

Note. JavaScriptSerializer is the slowest serial JSON serializer I have ever tested. So I had to remove it from my tests because it took too much time (> 100 times slower).

In any case, this is easily solved using ServiceStack.Text JSON Serializer :

var response = JsonSerializer.DeserializeFromString<Dictionary<string,string>>(responseFromServer);
var permissions = JsonSerializer.DeserializeFromString<Dictionary<string,string>>(response["result"]);
Console.WriteLine(response["flag"] + ":" + permissions["ServicePermission"]);

For completeness, this will also work with ServiceStack.Text.JsonSerializer:

public class Response
{
    public bool flag { get; set; }
    public string message { get; set; }
    public Permisions result { get; set; }
}
public class Permisions
{
    public bool ServicePermission { get; set; }
    public bool UserGroupPermission { get; set; }
}

var response = JsonSerializer.DeserializeFromString<Response>(responseFromServer);
Console.WriteLine(response.flag + ":" + response.result.ServicePermission);
+5
source
    if u are using jQuery u can do this

    var json=jQuery.parseJSON(responseFromServer);

    //acess
    alert(json.ServicePermission);

if you are asing microsoft ajax do this

var json=Sys.Serialization.JavaScriptSerializer.deserialize(responseFromServer,true);

    //acess
    alert(json.ServicePermission);

#, php, , json . #, .

:

//

public class Response
{
    public bool flag { get; set; }
    public string message { get; set; }
    public Permisions result { get; set; }
}
public class Permisions
{
    public bool ServicePermission { get; set; }
    public bool UserGroupPermission { get; set; }
}


        var responseFromServer =
            // lines split for readability
  "{\"flag\":true,\"message\":\"\",\"result\":{\"ServicePermission\":true,"
  + "\"UserGroupPermission\":true}}";
        var serializer = new System.Web.Script.Serialization.JavaScriptSerializer();
        var responseValue = serializer.Deserialize<Response>(responseFromServer);

    //access     
    responseValue.result.ServicePermission
-1

Source: https://habr.com/ru/post/1784736/


All Articles