搞定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);
}
}
}
复制代码

 

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


posted @   数据酷软件  阅读(4760)  评论(4编辑  收藏  举报
编辑推荐:
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
点击右上角即可分享
微信分享提示