简体   繁体   中英

Parse JSON object in C# without using class

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?

The method which you are using (ie:)

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

returns a JsonResult object which has a property named Data , ie 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.

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();

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM