简体   繁体   中英

how to get the values in json array in mvc3 controller?

I have a result that contains Json result. The Json Result are as follows.

      <string xmlns="http://tempuri.org/">
        {"Method":"LOGIN_AUTHENTICATE Start :8/29/2013 1:23:46 AM 
          ASW :8/29/2013 1:23:46 AM P21 :8/29/2013 1:23:50 AM End :8/29/2013 1:23:50AM",
        "ResponseCode":0,"ResponseText":"","HomeBannerURL":"http://example.com/example/",
        "resAccount":[{"shopper_uid":1877,"customer_code":"10950",
         "customer_name":"JASPER ACCOUNT",
         "contact_id":6449,"first_name":"jasper","last_name":"manickaraj",
         "email_address":"exam@example.com","password_hint":"name",
         "default_shipping_method_uid":110,"password":"abc123",
         "default_ship_to_address_id":"150"}],
         "resCategories":
 [{"item_category_uid":123,
        "item_category_desc":"EFG",
        "Total":0,"sub_category_image":"",
        "sub_category_thumb":""},
 {"item_category_uid":1,
        "item_category_desc":"ABC",
        "Total":0,"sub_category_image":"",
        "sub_category_thumb":""},
 {"item_category_uid":2,
        "item_category_desc":"BCD",
        "Total":0,"sub_category_image":"",
        "sub_category_thumb":""},
 {"item_category_uid":3,
        "item_category_desc":"CDE",
        "Total":0,"sub_category_image":"",
        "sub_category_thumb":""}]}
    <string>

Now i assign the above result to var Jsonresult; Now i need to get the customer_name, first_name from Jsonresult.. How to get the result.. Please help me to fix this..

Use JavaScriptSerializer to implement this feature. First,you must define a few entity,eg.

public class RequestObj
{
    public string Method { get; set; }
    public string ResponseCode { get; set; }
    public string HomeBannerURL { get; set; }

    public IList<Account> ResAccount { get; set; }
}

public class Account
{

    public string shopper_uid { get; set; }

    public string customer_name { get; set; }

    public string first_name { get; set; }
}

second,you can deserialize your string.

var scriptSerializer = new JavaScriptSerializer();
var obj = scriptSerializer.Deserialize<RequestObj>(str);

好的,您必须在项目中添加json.net dll,并这样编写代码:

var obj = JsonConvert.DeserializeObject<Dictionary<string, object>>(str.ToString());

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