6

I have two string values in a JSON object. I want to call this method in same class and use the values without using class.

I am using the following method:

public JsonResult Details()
{
    return Json(new { Data = "DisplayName", result = "UniqueName" });
}

I need to use this data and result value in another method.

I am getting the value like:

var Details = JsonConvert.SerializeObject(Details());

My output is:

{
    \"ContentEncoding\": null,
    \"ContentType\": null,
    \"Data\": {
        \"Data\": \"DisplayName\",
        \"result\": \"UniqueName\"
    },
    \"JsonRequestBehavior\": 1,
    \"MaxJsonLength\": null,
    \"RecursionLimit\": null
}

How do I get the data and result value from this?

5
  • Be specific. Show what you have tried so far and post a clear problem statement. Commented Jan 6, 2016 at 5:11
  • I have tried by below method to get data value. But I am getting the error which is also I mentioned below. var Details = JsonConvert.SerializeObject(Details()); Details["Data"]; 'string' does not contain a definition for 'data' and no extension method 'data' accepting a first argument of type 'string' could be found (are you missing a using directive or an assembly reference?) details["Data"] Commented Jan 6, 2016 at 5:14
  • Serializing will give you string, if you want to get something like property and it's value kind, then the data need to be in the object form. Try deserializing that string. Commented Jan 6, 2016 at 5:15
  • But I don't need any class to deserialize it. How to deserialize this without using class ? Commented Jan 6, 2016 at 5:17
  • See stackoverflow.com/q/3142495/3788456 Commented Jan 6, 2016 at 5:23

3 Answers 3

16

The method which you are using (i.e:)

public JsonResult Details()
{
    return Json(new { Data = "DisplayName", result = "UniqueName" });
}

returns a JsonResult object which has a property named Data, i.e Details().Data, which contains the data your object contains. So in order to get your object's Data and result values, you need to serialize it again.

This is the full solution:

JsonResult json = Details();                         // returns JsonResult type object
string ser = JsonConvert.SerializeObject(json.Data); // serializing JsonResult object (it will give you json string)
object dec = JsonConvert.DeserializeObject(ser);     // deserializing Json string (it will deserialize Json string)
JObject obj =  JObject.Parse(dec.ToString());        // it will parse deserialize Json object
string name = obj["Data"].ToString();                // now after parsing deserialize Json object you can get individual values by key i.e.

string name = obj["Data"].ToString();       // will give Data value
string name = obj["result"].ToString();     // will give result value

Hope this helps.

Sign up to request clarification or add additional context in comments.

Comments

9

By looking at JsonConvert.SerializeObject, I guess you are using NewtonSoft dll. In that you have JObject.Parse under Newtonsoft.Json.Linq which you can import (using Newtonsoft.Json.Linq;). You can parse that json string as

var details = JObject.Parse(your_json_string);

This will give you JObject and you can get the details as

var data = details["Data"].ToString();

2 Comments

Hi, If I have multiple data inside the json object, How to get that value ?? public JsonResult Details() { return Json( new { Data = "{'displayname':'display name 1','name':'Unique name 1'}, {'displayname':'display name 2','name':'Unique name 2'}" }); }
Even I have to do by trying it. I am not so much familiar with this.
1

JsonResult already stores the object for you, under Data. It has not been serialized yet, it simply indicates to the MVC framework to serialize it to JSON when responding to a web request.

var details = Details().Data;

Of course, this will be typed as an object - which isn't too useful. You can cast it back to the anonymous type like this:

private T CastToAnonymous<T>(object obj, T anonymousType)
{
    return (T)obj;
}

var details = CastToAnonymous(Details().Data, 
     new { Data = string.Empty, result = string.Empty });

And then you can use it like...

var data = details.Data;
var result  = details.result;

And it will be type-safe.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.