C# 如何反序列化JSON文本?
我有一个使用Json数据的项目,我尝试反序列化Json数据,如下所示:C# 如何反序列化JSON文本?,c#,json,C#,Json,我有一个使用Json数据的项目,我尝试反序列化Json数据,如下所示: [{"232":{"id":"232","reference":"022222","name":"Poire","content_title":"","content":"","pv_ttc":"230","picture_1":"","picture_2":"","picture_3":"","picture_4":"","picture_5":""}}] Product product = new Product();
[{"232":{"id":"232","reference":"022222","name":"Poire","content_title":"","content":"","pv_ttc":"230","picture_1":"","picture_2":"","picture_3":"","picture_4":"","picture_5":""}}]
Product product = new Product();
product.Name = "Apple";
product.ExpiryDate = new DateTime(2008, 12, 28);
product.Price = 3.99M;
product.Sizes = new string[] { "Small", "Medium", "Large" };
string output = JsonConvert.SerializeObject(product);
//{
// "Name": "Apple",
// "ExpiryDate": "2008-12-28T00:00:00",
// "Price": 3.99,
// "Sizes": [
// "Small",
// "Medium",
// "Large"
// ]
//}
Product deserializedProduct = JsonConvert.DeserializeObject<Product>(output);
如果我正确理解Json,一开始我们有一个索引,然后是一个带有名称、参考、价格等的子板
那么,如何将此文本反序列化为对象
知道我的班级是这样的:
public class productClass
{
public string id {get;set;}
public string reference { get; set; }
public string name { get; set; }
public string content_title{ get; set; }
public string content { get; set; }
public float pv_ttc{get;set;}
public string picture_1{get;set;}
public string picture_2{get;set;}
public string picture_3{get;set;}
public string picture_4{get;set;}
public string picture_5{get;set;}
public List<productClass> urlResult;
public productClass ( )
{
}
public productClass (string _id, string _reference, string _name, string _content_title, string _content, float _pv_ttc, string _picture_1, string _picture_2, string _picture_3, string _picture_4, string _picture_5)
{
id = _id;
reference = _reference;
name = _name;
content_title = _content_title;
content = _content;
pv_ttc = _pv_ttc;
picture_1 = _picture_1;
picture_2 = _picture_2;
picture_3 = _picture_3;
picture_4 = _picture_4;
picture_5 = _picture_5;
urlResult = new List<productClass> ( );
}
public void addUrl ( List<productClass> urlResult )
{
foreach ( productClass _url in urlResult )
{
urlResult.Add ( _url );
}
}
}
公共类productClass
{
公共字符串id{get;set;}
公共字符串引用{get;set;}
公共字符串名称{get;set;}
公共字符串内容\u标题{get;set;}
公共字符串内容{get;set;}
公共浮点pv_ttc{get;set;}
公共字符串picture_1{get;set;}
公共字符串picture_2{get;set;}
公共字符串picture_3{get;set;}
公共字符串picture_4{get;set;}
公共字符串picture_5{get;set;}
公开结果清单;
公共产品类别()
{
}
公共产品类(字符串id、字符串引用、字符串名称、字符串内容标题、字符串内容、浮点值、字符串图片1、字符串图片2、字符串图片3、字符串图片4、字符串图片5)
{
id=_id;
参考=_参考;
名称=_名称;
内容\标题=\内容\标题;
内容=_内容;
pv_ttc=_pv_ttc;
图片_1=_图片_1;
picture_2=_picture_2;
图片_3=_图片_3;
picture_4=_picture_4;
图片_5=_图片_5;
urlResult=新列表();
}
public void addUrl(列表URL结果)
{
foreach(urlResult中的productClass\uURL)
{
urlResult.Add(_url);
}
}
}
谢谢您的帮助。@sachou您考虑过使用JSON.Net吗?这是一个非常好的.Net框架,您可以轻松地将JSON字符串反序列化到C#对象中。您可以查看文档,但以下是网站上的一个简单示例:
Product product = new Product();
product.Name = "Apple";
product.ExpiryDate = new DateTime(2008, 12, 28);
product.Price = 3.99M;
product.Sizes = new string[] { "Small", "Medium", "Large" };
string output = JsonConvert.SerializeObject(product);
//{
// "Name": "Apple",
// "ExpiryDate": "2008-12-28T00:00:00",
// "Price": 3.99,
// "Sizes": [
// "Small",
// "Medium",
// "Large"
// ]
//}
Product deserializedProduct = JsonConvert.DeserializeObject<Product>(output);
产品产品=新产品();
product.Name=“苹果”;
product.ExpiryDate=新日期时间(2008,12,28);
产品价格=399万元;
product.size=新字符串[]{“小”、“中”、“大”};
字符串输出=JsonConvert.SerializeObject(产品);
//{
//“名称”:“苹果”,
//“到期日”:“2008-12-28:00:00”,
//“价格”:3.99,
//“尺寸”:[
//“小”,
//“中等”,
//“大”
// ]
//}
Product deserializedProduct=JsonConvert.DeserializeObject(输出);
我希望这会有所帮助。我建议您使用JSON框架,如 您可以非常轻松地序列化和反序列化JSON对象,如下所示:
[{"232":{"id":"232","reference":"022222","name":"Poire","content_title":"","content":"","pv_ttc":"230","picture_1":"","picture_2":"","picture_3":"","picture_4":"","picture_5":""}}]
Product product = new Product();
product.Name = "Apple";
product.ExpiryDate = new DateTime(2008, 12, 28);
product.Price = 3.99M;
product.Sizes = new string[] { "Small", "Medium", "Large" };
string output = JsonConvert.SerializeObject(product);
//{
// "Name": "Apple",
// "ExpiryDate": "2008-12-28T00:00:00",
// "Price": 3.99,
// "Sizes": [
// "Small",
// "Medium",
// "Large"
// ]
//}
Product deserializedProduct = JsonConvert.DeserializeObject<Product>(output);
产品产品=新产品();
product.Name=“苹果”;
product.ExpiryDate=新日期时间(2008,12,28);
产品价格=399万元;
product.size=新字符串[]{“小”、“中”、“大”};
字符串输出=JsonConvert.SerializeObject(产品);
//{
//“名称”:“苹果”,
//“到期日”:“2008-12-28:00:00”,
//“价格”:3.99,
//“尺寸”:[
//“小”,
//“中等”,
//“大”
// ]
//}
Product deserializedProduct=JsonConvert.DeserializeObject(输出);
仔细看一下这是我的例子。我以谷歌地图api为例 我创建了以下与GoogleMaps对应的类
public class AddressComponent
{
public string long_name { get; set; }
public string short_name { get; set; }
public List<string> types { get; set; }
}
public class Result
{
public List<AddressComponent> address_components { get; set; }
public List<string> types { get; set; }
}
public class RootObject
{
public List<Result> results { get; set; }
public string status { get; set; }
}
公共类地址组件
{
公共字符串长_名称{get;set;}
公共字符串短_名称{get;set;}
公共列表类型{get;set;}
}
公开课成绩
{
公共列表有关更多信息,请参阅我的反序列化方法:
public IEnumerator loadProducts (int cat)
{
List <int> catNoTri = new List<int> ();
catNoTri.Add (0);
gm.totalPriceItem.Clear();
isLoading = true;
WWW www = new WWW ( urlProduct );
yield return www;
Debug.Log(www.text);
string json = www.text.ToString ( );
IndexPrductClass myJson = JsonReader.Deserialize<IndexPrductClass> ( json );
foreach (productClass item in products)
{
for (int i = 0; i < products.Length; i++)
{
Debug.Log("product.productValue[i] = " + products[i].name);
}
if (firstLoad || gm.catId == 0)
{
Debug.Log ("here1");
nameProduct.Add (item.name);
Debug.Log("item.name = " + item.name);
idProduct.Add (item.id);
prixItem.Add (item.pv_ttc);
Debug.Log("item.pv_ttc = " + item.pv_ttc);
gm.totalPriceItem.Add (0);
gm.qte = new int [gm.totalPriceItem.Count];
descriptifProduct.Add (item.content);
Debug.Log(" item.content = " +item.content);
}
else if (!firstLoad)
{
Debug.Log ("here2");
nameProduct.Add (item.name);
idProduct.Add (item.id);
prixItem.Add (item.pv_ttc);
gm.totalPriceItem.Add (0);
gm.qte = new int [gm.totalPriceItem.Count];
descriptifProduct.Add (item.content);
}
}
gm.canLoad = true;
}
public IEnumerator loadProducts(int cat)
{
List catNoTri=新列表();
catNoTri.Add(0);
gm.totalPriceItem.Clear();
isLoading=true;
WWW=新的WWW(urlProduct);
收益率;
Debug.Log(www.text);
字符串json=www.text.ToString();
IndexPrductClass myJson=JsonReader.Deserialize(json);
foreach(产品中的productClass项)
{
对于(int i=0;i
这是一个评论,而不是一个答案。我知道Json.Net,但我是学生,我的培训师要求我在不安装插件或framworks的情况下完成这个项目,简而言之,我必须用我拥有的来管理。那么,如何将此文本反序列化为对象?
搜索“”在你最喜欢的搜索引擎上,你无疑会找到大量的资源。JSON.Net是实现这一点的最佳库之一。不要尝试自己从字符串中解析JSON。这可能是一个注释而不是答案,与@BazindrixI的现有答案几乎完全相同。很抱歉,这实际上需要一些时间我写答案的时候,有人在网站上发布了相同的示例。我无法预见未来我使用您的解决方案,但编译器返回我:InvalidCastException:Cannot cast from source type to destination type.JsonFx.Json.JsonReader.Deserialize[indexpuctclass](System.String value)
。很抱歉,这是我第一次使用Json数据,我有点迷路。是否使用Newtonsoft.Json添加程序集引用?请检查此处的代码,以便下载