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?

Renan Araújo
  • 3,533
  • 11
  • 39
  • 49
Shesha
  • 1,857
  • 6
  • 21
  • 28
  • Be specific. Show what you have tried so far and post a clear problem statement. – Mangesh Jan 06 '16 at 05: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"] – Shesha Jan 06 '16 at 05: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. – Bharadwaj Jan 06 '16 at 05:15
  • But I don't need any class to deserialize it. How to deserialize this without using class ? – Shesha Jan 06 '16 at 05:17
  • See http://stackoverflow.com/q/3142495/3788456 – AJ X. Jan 06 '16 at 05:23

3 Answers3

15

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.

CarenRose
  • 1,266
  • 1
  • 12
  • 24
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();

Bharadwaj
  • 2,535
  • 1
  • 22
  • 35
  • 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'}" }); } – Shesha Jan 06 '16 at 08:38
  • Even I have to do by trying it. I am not so much familiar with this. – Bharadwaj Jan 06 '16 at 09:52
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.

Rob
  • 26,989
  • 16
  • 82
  • 98