如何在Windows应用程序中处理json响应 [英] How to handle json response in windows application
本文介绍了如何在Windows应用程序中处理json响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
使用系统;
使用 System.Collections.Generic;
使用 System.Linq;
使用 System.Web;
使用 System.Web.Services;
使用 System.Web.Script.Services;
使用 System.Web.Script.Serialization;
使用 MySql.Data.MySqlClient;
使用 System.Data;
/// < 摘要 >
/// loginvalidation的摘要说明
/// < / summary >
///
公开 类客户
{
public int id { get ; set ; }
public string name { get 跨度>; set ; }
public int uid { get 跨度>; set ; }
}
public class loginvalidation
{
connection_class c = new connection_class();
MySqlCommand cmd;
MySqlDataReader dr;
MySqlConnection con;
[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public string validatelogin( string username, string password)
{
customer [] cus = new customer []
{
new customer()
{
id = 101 ,
name = Eswar,
uid = 3473
}
};
return new JavaScriptSerializer()。Serialize(cus);
}
}
我的Web服务输出:
< ; xml version = 1.0 编码 = UTF-8 ? >
< string xmlns = http://tempuri.org/ > [{id :101,name:Eswar,uid:3473}] < / string >
任何人都可以帮助我???如何在我的C#windows应用程序中获取此响应并打印到文本框字段
解决方案
请按照以下流程进行操作:
步骤1:添加对Windows应用程序的服务引用。
步骤2:添加引用System.Web.Extensions;
步骤3:使用System.Web.Script.Serialization添加命名空间;
步骤4:添加以下代码:
WebService1SoapClient cc = new WebService1SoapClient();
string test = cc.validatelogin();
customer [] deserialized = new JavaScriptSerializer()。反序列化< customer [] > < /跨度>(测试);
foreach (客户customerRec 反序列化)
{
int id = customerRec.id;
string name = customerRec.name;
int uid = customerRec.uid;
}
注意:确保客户类在Windows项目中也可用,因为在进行反序列化时需要它。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Script.Services;
using System.Web.Script.Serialization;
using MySql.Data.MySqlClient;
using System.Data;
/// <summary>
/// Summary description for loginvalidation
/// </summary>
///
public class customer
{
public int id { get; set; }
public string name { get; set; }
public int uid { get; set; }
}
public class loginvalidation
{
connection_class c=new connection_class();
MySqlCommand cmd;
MySqlDataReader dr;
MySqlConnection con;
[WebMethod]
[ScriptMethod(ResponseFormat=ResponseFormat.Json)]
public string validatelogin(string username,string password)
{
customer[] cus=new customer[]
{
new customer()
{
id=101,
name="Eswar",
uid=3473
}
};
return new JavaScriptSerializer().Serialize(cus);
}
}
My Webservice Output:
<?xml version="1.0" encoding="UTF-8"?>
<string xmlns="http://tempuri.org/">[{"id":101,"name":"Eswar","uid":3473}]</string>
Can anyone help me ??? How to get this response in my C# windows application and print into text box fields
解决方案
Please follow below process:
Step1: Add service reference to Windows App.
Step2: Add reference System.Web.Extensions;
Step3: Add namespace using System.Web.Script.Serialization;
Step4: Add below code:
WebService1SoapClient cc = new WebService1SoapClient(); string test = cc.validatelogin(); customer[] deserialized = new JavaScriptSerializer().Deserialize<customer[]>(test); foreach (customer customerRec in deserialized) { int id = customerRec.id; string name = customerRec.name; int uid = customerRec.uid; }
Note: Make sure that customer class is available in Windows project also because it is required when we do deserializeation.
这篇关于如何在Windows应用程序中处理json响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文