将json格式解析为字符串格式 [英] parsing json format to string format

查看:128
本文介绍了将json格式解析为字符串格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这里我有json格式的数据。请告诉我如何将其转换为字符串格式。

 public string Email_address =; 
公共字符串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;








/ *我没有使用任何JSON解析器,因为我不想要使用任何额外的dll /第三方dll * /
使用(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}
* /

数组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).T oString();
Google_ID = ar.GetValue(0).ToString();
firstName = ar.GetValue(4).ToString();
LastName = ar.GetValue(5).ToString();
// Session [UName] = ar.GetValue(1).ToString();




}

解决方案

那里是内置类,允许您序列化/反序列化JSON对象。



请参阅 JSONSerializer [ ^ ]







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






将JSON字符串粘贴到以下工具中: json2csharp [ ^ ],这将生成ac#适合你的课程。



我假设你没有使用JSON.NET。如果是这种情况,那么你可以试试 [ ^ ]。



它具有以下功能 -



1. LINQ to JSON

2.用于快速将.NET对象转换为JSON并再次返回的JsonSerializer

3.Json.NET可以选择生成格式良好,缩进的JSON用于调试或显示

4.可以将类似JsonIgnore和JsonProperty的属性添加到类中以自定义类的序列化方式

5.转换能力来自XML的JSON

6.支持多种平台:.NET,Silverlight和Compact Framework



看看示例 [ ^ ]如下。在此示例中, JsonConvert [ ^ ]对象用于将对象转换为JSON和从JSON转换对象。它有两种静态方法用于此目的。它们是 SerializeObject(Object obj) [ ^ ]和DeserializeObject<t>(String json) [ ^ ] -



产品产品=  new  Product(); 
product.Name = Apple;
product.Expiry = new DateTime( 2008 12 28 );
product.Price = 3 .99M;
product.Sizes = new string [] { Medium Large};

string json = JsonConvert.SerializeObject(product);
// {
/ / 名称:Apple,
// Expiry:2008-12-28T00:00:00,
// Price:3.99,
// 尺寸 :[
// 小,
// Medium,
// Large
// ]
// }

Product deserializedProduct = JsonConvert.DeserializeObject<产品>(JSON);


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




           }

解决方案

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)[^] -

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


这篇关于将json格式解析为字符串格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆