3
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 is get a webservice, and then how to get the JSON string value, such as "flag","Servicepermission"??

affix: i'm sorry, using c# to do this.

2 Answers 2

5

Note: The JavaScriptSerializer is actually the slowest JSON Serializer I've ever benchmarked. So much so I've had to remove it from my benchmarks because it was taking too long (>100x slower).

Anyway this easily solved using ServiceStack.Text's 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 would 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);
Sign up to request clarification or add additional context in comments.

1 Comment

+1 for the answer but the link to the JSON Serializer is broken. This is the updated url: github.com/ServiceStack/ServiceStack.Text
-1
    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);

in c# like php i have'nt seen any method that converts json to object on the fly. To do conversions in c# you must first create a class for this.

For your case you can do like this

//define classes

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

2 Comments

i'm sorry, using c# to do this.
yes, it's. i have a webservice such as Authentication.ashx,and i using c# in default.aspx.cs,i can get the result, and then want to parse the json string.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.