Json数组到结构(C#) [英] Json Array to Struct (C#)

查看:119
本文介绍了Json数组到结构(C#)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我得到了这个Json数据:

 {
1:{currency_code:USD,currency_name:USDOLLAR,购买:18990,transfer:18990,sell:19050},
2:{currency_code:EUR,currency_name:EURO,买:23596.94,转让:23667.94,卖出:23981.34},
}





我声明这样的结构:



  public   class 货币
{
public string currency_code { get ; set ; }
public string currency_name { get ; set ; }
public string buy { get ; set ; }
public string transfer { get ; set ; }
public string sell { get ; set ; }
}





我尝试使用Newtonsoft.Json dll,但仍然无法将json数组转换为我的结构。



任何人都有线索或示例代码?

解决方案

您的json无法直接转换为货币数组,因为它是键值对的形式。



我发现有一个额外的逗号在json中添加了最后一个产生问题的内容。





我尝试将提供的json转换为 Dictionary< string,Currency> ,我从json中删除了错误的逗号后获得了成功。



试试这个。



 string json ={+ 
\1 \\ \\ :{\ currency_code\ :\ USD\ \ currency_name\:\ USDOLLAR\,\ buy\:\ 18990\ \ transfer\:\ 18990\ ,\sell\:\19050 \},+
\2 \:{\currency_code \:\EUR \ \ currency_name\:\ EURO\,\ buy\:\ 23596.94\,\ transfer\:\ 23667.94\,\\ \\sell \:\23981.34 \}+
};


字典<字符串,货币> list =
new JavaScriptSerializer()。Deserialize< Dictionary< string,Currency>>(json);


谢谢Prakash Kalakoti :-D



最后','是我的错,有r USD,EUR,HKD,AUD,bla bla但我只是复制其中的两个,我忘了删除最后一个','; P


backstr = wr.GetGroup( SIJ-kjsgEoG9CtE9Rm8ZiyiwuL3-58K1fZlmXIU5iyYVEFoDJ7d0Idn3-bOooAf1GDIzrJua7Am0yZDXW3bWOUuK_xa6eBELSNhmwLaxhoX3C34g9HvYCf9BTOk9bBukFaLNsoU2djsRfm0M5UHPmw);

I got this Json data:

{
"1":{"currency_code":"USD","currency_name":"USDOLLAR","buy":"18990","transfer":"18990","sell":"19050"},
"2":{"currency_code":"EUR","currency_name":"EURO","buy":"23596.94","transfer":"23667.94","sell":"23981.34"},
}



I declare struct like this:

public class Currency
        {
            public string currency_code { get; set; }
            public string currency_name { get; set; }
            public string buy { get; set; }
            public string transfer { get; set; }
            public string sell { get; set; }
        }



I tried with Newtonsoft.Json dll but still cant convert that json array to my struct.

Anybody got a clue or an example code?

解决方案

Your json can't be directly converted to Currency array as it is in the form of key value pair.

I have found that there is one extra comma "," got added in the last for the json which is creating the problem.


I tried converting the provided json to Dictionary<string, Currency> and I got success after removing the incorrect comma from the json.

try this.

string json = "{" +
                      "\"1\":{\"currency_code\":\"USD\",\"currency_name\":\"USDOLLAR\",\"buy\":\"18990\",\"transfer\":\"18990\",\"sell\":\"19050\"}," +
                      "\"2\":{\"currency_code\":\"EUR\",\"currency_name\":\"EURO\",\"buy\":\"23596.94\",\"transfer\":\"23667.94\",\"sell\":\"23981.34\"}" +
                      "}";

       
 Dictionary<string, Currency> list =
          new JavaScriptSerializer().Deserialize<Dictionary<string, Currency>>(json);


thank you Prakash Kalakoti :-D

the last ',' is my fault, there r USD, EUR, HKD, AUD, bla bla but i just copy 2 of them and i forgot to remove the last ',' ;P


backstr = wr.GetGroup("Sij-kjsgEoG9CtE9Rm8ZiyiwuL3-58K1fZlmXIU5iyYVEFoDJ7d0Idn3-bOooAf1GDIzrJua7Am0yZDXW3bWOUuK_xa6eBELSNhmwLaxhoX3C34g9HvYCf9BTOk9bBukFaLNsoU2djsRfm0M5UHPmw");


这篇关于Json数组到结构(C#)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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