搞定2个问题:C# 动态调用java webservice,Winform解析Json字符串中特殊值

FAQ:

1、最近做的项目需要和Java做的系统的webservice交互数据.一上来直接添加web引用,一路OK。但是地址在发布时要变的,以为也简单,其实不然,改了app.config里的配置,调用代理类的方法,结果一直不变。郁闷了。google了一把解决了。原来不能直接实例化代理类。需要如下处理:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using GB_PRINT.com.goodbabygroup.factorybarcode_sayHi;
using GB_PRINT.com.goodbabygroup.factorybarcode;
using GB_PRINT.com.goodbabygroup.factorybarcode_Login;

namespace GB_PRINT
{
/// <summary>
/// HelloWorld动态代理类
/// </summary>
public class wsHelloWorldProxy : HelloWorld
{
public wsHelloWorldProxy(string url)
{
this.Url = "http://" + url + "/ws/webservice/helloWorld";
}
}

/// <summary>
/// StoreProcedure动态代理类
/// </summary>
public class wsStoreProcProxy : StoreProcedure
{
public wsStoreProcProxy(string url)
{
this.Url = "http://" + url + "/ws/storeProcedure";
}
}

/// <summary>
/// StoreProcedure动态代理类
/// </summary>
public class wsLoginProxy : CsLoginService
{
public wsLoginProxy(string url)
{
this.Url = "http://" + url + "/ws/csLogin";
}
}
}

2、Json数据的解析。

string result =@"{"Own_Gp_Num_Id":"13633","Empe_Name":"推车一厂","Message":"登录成功","Empe_Id":"500001","ORG_NO_Value":"儿童用品有限公司推车一厂-打印中心","SubEntity_Name":"儿童用品有限公司推车一厂","ENTITY_NAME":"儿童用品有限公司推车一厂","CLIENT_MAC":"cb159d4b7eadf88166bc0e6ff23f5314","popedomInfo":[ ],"Cort_Num_Id":"9586","Code":"0","ORG_NO_ID":"6"}";

popedomInfo":[ ],"在Json里面代码个数组。我刚开始使用第一个版本的类去接,老是报什么"System.String"什么转换错误,后来发现js里面[]代表一个数组。修改后成功。

第一版的:

/// <summary>
/// 用户实体DTO
/// </summary>
public class LocalUserEntity
{
public string Own_Gp_Num_Id
{
get;
set;
}
public string Empe_Name
{
get;
set;
}
public string Empe_Id
{
get;
set;
}
public string ORG_NO_Value
{
get;
set;
}
public string SubEntity_Name
{
get;
set;
}
public string ENTITY_NAME
{
get;
set;
}
public string CLIENT_MAC
{
get;
set;
}
public string popedomInfo
{
get;
set;
}
public long Cort_Num_Id
{
get;
set;
}
public long ORG_NO_ID
{
get;
set;
}

public string Code
{
get;
set;
}

public string Message
{
get;
set;
}
}

第二版:

/// <summary>
/// 用户实体DTO
/// </summary>
public class LocalUserEntity
{
public string Own_Gp_Num_Id
{
get;
set;
}
public string Empe_Name
{
get;
set;
}
public string Empe_Id
{
get;
set;
}
public string ORG_NO_Value
{
get;
set;
}
public string SubEntity_Name
{
get;
set;
}
public string ENTITY_NAME
{
get;
set;
}
public string CLIENT_MAC
{
get;
set;
}
public int [] popedomInfo
{
get;
set;
}
public long Cort_Num_Id
{
get;
set;
}
public long ORG_NO_ID
{
get;
set;
}

public string Code
{
get;
set;
}

public string Message
{
get;
set;
}
}


调用:

LocalUserEntity result = null;
try
{
jsonData_str = Program.csLogin.csLogin(vParam);
result = JsonConvert.DeserializeObject<LocalUserEntity>(jsonData_str);
}
catch (Exception ex)
{
throw ex;
}
//假装授权
result.popedomInfo = new int[] { 1,2,3 };
if (result.Message.Equals("登录成功"))
{
if (result.popedomInfo.Length > 0)
{
foreach(int popedomValue in result.popedomInfo)
{
ClientPopedom value = (ClientPopedom)Enum.Parse(typeof(ClientPopedom), popedomValue.ToString());
LocalPopedom.m_LocalPopedom.clientPopedom.Add(value);
}
}
}

 

记录下来备忘,也给遇到同样问题的朋友一个很逊的方案。 


发布了162 篇原创文章 · 获赞 44 · 访问量 19万+

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 大白 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览