搞定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);
}
}
}
记录下来备忘,也给遇到同样问题的朋友一个很逊的方案。
作者:数据酷软件
出处:https://www.cnblogs.com/datacool/archive/2012/03/28/Josn001797110.html
关于作者:20年编程从业经验,持续关注MES/ERP/POS/WMS/工业自动化
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明。
联系方式: qq:71008973;wx:6857740733
基于人脸识别的考勤系统 地址: https://gitee.com/afeng124/viewface_attendance_ext
自己开发安卓应用框架 地址: https://gitee.com/afeng124/android-app-frame
WPOS(warehouse+pos) 后台演示地址: http://47.239.106.75:8080/
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 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语句:使用策略模式优化代码结构