简体   繁体   中英

How to get values from Json String with the provided format

I am converting the datatable into Json string using

return JsonConvert.SerializeObject(ds.Tables[0]);

and i am receiving the following string.

{{
  "CheckUserResult": "{\"Table\":[{\"User_ID\":4,\"User_Name\":\"IKERocks\",\"User_Email\":\"bamane1989@gmail.com\",\"User_PhoneNumber\":\"8452046777\",\"User_DateOfBirth\":\"\\/Date(1485845400000+0530)\\/\",\"User_City\":\"Pune\",\"User_Admin\":null,\"User_Points\":10,\"User_Login_Provider\":\"Google\"}]}"
}}

now I need to retrieve the values into my object.

Userdetails.User_Email = Convert.ToString(userdata["User_Email"])

I am getting null always.

whats the correct syntax to get the key value pair?

you have to build a class

public class UserDetails
{  
    public int User_ID{  
        get;  
        set;  
    }  
    public string User_Name{  
        get;  
        set;  
    }  
    public string User_Email{  
        get;  
        set;  
    }  
   ....
}  

string jsonData = "{\'User_ID\':4,\'User_Name\':\'IKERocks\',\'User_Email\':\'bamane1989@gmail.com\'}";
var myDetails = JsonConvert.DeserializeObject<UserDetails>(jsonData);

and after you can do this

String Username = myDetails.User_Name;

Userdetails.User_Email = myDetails.User_Email

this code maybe help you :

public string DataTableToJSONWithJavaScriptSerializer(DataTable table) 
{  
    JavaScriptSerializer jsSerializer = new JavaScriptSerializer();  
    List < Dictionary < string, object >> parentRow = new List < Dictionary < string, object >> ();  
    Dictionary < string, object > childRow;  
    foreach(DataRow row in table.Rows) 
    {  
        childRow = new Dictionary < string, object > ();  
        foreach(DataColumn col in table.Columns) 
        {  
            childRow.Add(col.ColumnName, row[col]);  
        }  
        parentRow.Add(childRow);  
    }  
    return jsSerializer.Serialize(parentRow);  
}  

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