Click here to Skip to main content
14,883,063 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Here I have the data with format of json.Please let me know how to convert it in to string format.
public string Email_address = "";
       public string Google_ID = "";
       public string firstName = "";
       public string LastName = "";
       public string Client_ID = "";
       public string Return_url = "";

       protected void Page_Load(object sender, EventArgs e)
       {
           if (!IsPostBack)
           {
               Client_ID = ConfigurationSettings.AppSettings["google_clientId"].ToString();
               Return_url = ConfigurationSettings.AppSettings["google_RedirectUrl"].ToString();
           }



           if (Request.QueryString["access_token"] != null)
           {

               String URI = "https://www.googleapis.com/oauth2/v1/userinfo?access_token=" + Request.QueryString["access_token"].ToString();

               WebClient webClient = new WebClient();
               Stream stream = webClient.OpenRead(URI);
               string b;








               /*I have not used any JSON parser because I do not want to use any extra dll/3rd party dll*/
               using (StreamReader br = new StreamReader(stream))
               {

                   b = br.ReadToEnd();
               }

               b = b.Replace("id", "").Replace("email", "");
               b = b.Replace("given_name", "");
               b = b.Replace("family_name", "").Replace("link", "").Replace("picture", "");
               b = b.Replace("gender", "").Replace("locale", "").Replace(":", "");
               b = b.Replace("\"", "").Replace("name", "").Replace("{", "").Replace("}", "");

               /*

               "id": "109124950535374******"
                 "email": "usernamil@gmail.com"
                 "verified_email": true
                 "name": "firstname lastname"
                 "given_name": "firstname"
                 "family_name": "lastname"
                 "link": "https://plus.google.com/10912495053537********"
                 "picture": "https://lh3.googleusercontent.com/......./photo.jpg"
                 "gender": "male"
                 "locale": "en" }
              */

               Array ar = b.Split(",".ToCharArray());
               for (int p = 0; p < ar.Length; p++)
               {
                   ar.SetValue(ar.GetValue(p).ToString().Trim(), p);

               }
               Email_address = ar.GetValue(1).ToString();
               Google_ID = ar.GetValue(0).ToString();
               firstName = ar.GetValue(4).ToString();
               LastName = ar.GetValue(5).ToString();
               //Session["UName"] = ar.GetValue(1).ToString();




           }
Posted
Updated 20-Jan-14 20:07pm
v4

There are built-in classes that allow you to serialize/deserialze JSON objects.

See JSONSerializer[^]

and

http://msdn.microsoft.com/en-us/library/bb412170(v=vs.110).aspx[^]
   
Hi,

Paste you JSON string into the following tool: json2csharp[^], this will generate a c# class for you.

I am assuming you are not using JSON.NET. If this the case, then you can try it[^].

It has the following features -

1. LINQ to JSON
2. The JsonSerializer for quickly converting your .NET objects to JSON and back again
3. Json.NET can optionally produce well formatted, indented JSON for debugging or display
4. Attributes like JsonIgnore and JsonProperty can be added to a class to customize how a class is serialized
5. Ability to convert JSON to and from XML
6.Supports multiple platforms: .NET, Silverlight and the Compact Framework

Look at the example[^] below. In this example, JsonConvert[^] object is used to convert an object to and from JSON. It has two static methods for this purpose. They are SerializeObject(Object obj)[^] and DeserializeObject<t>(String json)[^] -

C#
Product product = new Product();
product.Name = "Apple";
product.Expiry = new DateTime(2008, 12, 28);
product.Price = 3.99M;
product.Sizes = new string[] { "Small", "Medium", "Large" };

string json = JsonConvert.SerializeObject(product);
//{
//  "Name": "Apple",
//  "Expiry": "2008-12-28T00:00:00",
//  "Price": 3.99,
//  "Sizes": [
//    "Small",
//    "Medium",
//    "Large"
//  ]
//}

Product deserializedProduct = JsonConvert.DeserializeObject<product>(json);
   
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900