如何从json访问动态密钥和值到wcf [英] how to access dynamic key and value from json to wcf

查看:66
本文介绍了如何从json访问动态密钥和值到wcf的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,





我从json传递requeset到wcf服务除了scoredata参数值之外我得到的所有参数值是动态的。请帮助我解决我的问题。



Json请求





{ProspectNo:1000000073,MakerId:C136771,MkrDate:2015/11/26,TranID:null,ScoreData:{客户体验:1,稳定性和所有权:7,财产所有权:22,共同申请/担保人:16,建议成本:1800000}}



  public   class 得分
{

[DataMember()]
public string ProspectNo;

[DataMember()]
public string MakerId;

[DataMember()]
public string MkrDate;


[DataMember()]
public string TranID;

[DataMember()]
public Dictionary< string,string> ScoreData;
}









 



MakerId =C136771

MkrDate =2015/11/26

ProspectNo =1000000073

ScoreData = Count = 0

解决方案

这是因为JSON(de)序列化程序正在寻找一个数组并且得到一个对象。



您可以通过以下两种方式解决此问题;第一个(也是最简单的)是更改ScoreData的类型以更恰当地反映传入的数据:



 [DataMember()] 
public object ScoreData;





当你需要阅读时,你可以使用动态。



另一条路线是纠正JSON即将投入使用。更正后的JSON如下所示:

 {  ProspectNo   1000000073  MakerId  C136771  MkrDate < span class =code-string> 2015/11/26,  TranID null   ScoreData: [{ 密钥 < span class =code-string>客户体验, :< span class =code-string>  1},{  Key 稳定性&所有权    7},{  Key  属性所有权   22},{   Co-app / Guarantor  Value  16},{ 密钥 建议成本  Va lue  1800000}]} 


Hi all,


I am passsing requeset from json to wcf service all the parameter value i get except scoredata parameter value which is dynamic. pls help me to solve my problem.

Json Request


{"ProspectNo":"1000000073","MakerId":"C136771","MkrDate":"2015/11/26","TranID":null,"ScoreData":{"Client Experience":"1","Stability & Ownership":"7","Property ownership":"22","Co-app/Guarantor":"16","proposed cost":"1800000"}}

public class Score
{

    [DataMember()]
    public string ProspectNo;

    [DataMember()]
    public string MakerId;

    [DataMember()]
    public string MkrDate;

  
    [DataMember()]
    public string TranID;

    [DataMember()]
   public Dictionary<string, string> ScoreData;
}






MakerId = "C136771"
MkrDate = "2015/11/26"
ProspectNo = "1000000073"
ScoreData = Count = 0

解决方案

That's because the JSON (de)serializer is looking for an array and is getting an object.

You can solve this in one of two ways; the first (and easiest) is to change the type of ScoreData to more appropriately reflect the incoming data:

[DataMember()]
   public object ScoreData;



You can then use "as dynamic" when you need to read from it.

The other route would be to correct the JSON that is coming into the service. The corrected JSON would look like:

{"ProspectNo":"1000000073","MakerId":"C136771","MkrDate":"2015/11/26","TranID":null,"ScoreData":[{ "Key": "Client Experience", "Value": "1"},{ "Key": "Stability & Ownership", "Value" ,"7"},{"Key": "Property ownership", "Value": "22"},{"Key": "Co-app/Guarantor", "Value":"16"},{ "Key": "proposed cost", "Value":"1800000"}]}


这篇关于如何从json访问动态密钥和值到wcf的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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