本文介绍了使用Json.net反序列化Json的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
解析此Json并获取值Usign Json.NET.帮助我找到解析的方法.在此先感谢.
parse this Json and Get The Values Usign Json.NET.Help Me to Find The Way To Parse.Thanks in Advance..
我正在使用Json.net Dll进行解析,以下是我要解析的Json代码和C#代码,但是我遇到了错误,无法从该Json文件中获取值.
I am Using Json.net Dll For Parsing,Below is my Json Code and C# code to Parse, But I am Getting Error, Confuced to Get the Values From This Json File.
myjson.json:
myjson.json:
{
"Published_DocsCount": 5,
"Public_ReadAccess": 2,
"Public_CommentAccess": 3,
"Public_WriteAccess": 2,
"Public_DocsCount": 7,
"PublicWithLink_ReadAccess": 2,
"PublicWithLink_CommentAccess": 2,
"PublicWithLink_WriteAccess": 2,
"PublicWithLink_DocsCount": 6,
"InternalCollaborators_CollaboratorsCount": 1,
"InternalCollaborators_Read": 3,
"InternalCollaborators_Comment": 0,
"InternalCollaborators_Write": 5,
"InternalCollaborators_DocsCount": 8,
"OutsideDomain_CollaboratorsCount": 6,
"OutsideDomain_Read": 6,
"OutsideDomain_Comment": 4,
"OutsideDomain_Write": 6,
"OutsideDomain_DocsCount": 16,
"Domain_ReadAccess": 3,
"Domain_CommentAccess": 2,
"Domain_WriteAccess": 2,
"Domain_DocsCount": 7,
"DomainWithLink_ReadAccess": 2,
"DomainWithLink_CommentAccess": 2,
"DomainWithLink_WriteAccess": 2,
"DomainWithLink_DocsCount": 6,
"MimeTypes": {
"image/png": 10,
"application/vnd.openxmlformats-officedocument.wordprocessingml.document": 9,
"application/vnd.google-apps.spreadsheet": 8,
"application/vnd.google-apps.document": 8,
"application/vnd.google-apps.presentation": 6,
"application/vnd.google-apps.drawing": 5,
"application/vnd.google-apps.form": 1
},
"SharedToDomains": {
"18009029392713646891": {
"users": 1,
"documents": 3
},
"neverfailbackup.biz": {
"users": 1,
"documents": 3
},
"00781621852878369984": {
"users": 1,
"documents": 3
},
"thedomainmigration.info": {
"users": 1,
"documents": 3
},
"liveonlinecloud.info": {
"users": 1,
"documents": 3
},
"domainmigration.info": {
"users": 1,
"documents": 1
}
},
"SharedFromDomains": {
"mydomainmigration.info": {
"users": 1,
"documents": 8
}
}
}}
myCode:
public void getSummaryJson()
{
string FilePath = "D:\\jsontxt.json";
string all = "";
StringBuilder sb = new StringBuilder();
using (StreamReader r = new StreamReader(FilePath))
{
string line;
while ((line = r.ReadLine()) != null)
{
sb.AppendLine(line);
}
all = sb.ToString();
var parsed = JObject.Parse(all);
var container = DeserializeFromJson<List<Attributes>>(parsed[]);
}
}
public class Attributes
{
[JsonProperty("MimeTypes")]
public string MimeTypes{ get; set; }
[JsonProperty("SharedToDomains")]
public string SharedToDomains{ get; set; }
[JsonProperty("SharedFromDomains")]
public string SharedFromDomains{ get; set; }
}
public class DataJsonAttributeContainer
{
public List<Attributes> attributes { get; set; }
}
public static T DeserializeFromJson<T>(string json)
{
T deserializedProduct = JsonConvert.DeserializeObject<T>(json);
return deserializedProduct;
}
推荐答案
您必须根据JSON结构对类型进行建模.这些类应该起作用:
You have to model your type according to the JSON structure. These classes should work:
public class AttributeContainer
{
public Dictionary<string,int> MimeTypes { get; set; }
public Dictionary<string,Domain> SharedToDomains { get; set; }
public Dictionary<string,Domain> SharedFromDomains { get; set; }
public int Published_DocsCount { get; set; }
public int Public_ReadAccess { get; set; }
public int Public_CommentAccess { get; set; }
public int Public_WriteAccess { get; set; }
public int Public_DocsCount { get; set; }
public int PublicWithLink_ReadAccess { get; set; }
public int PublicWithLink_CommentAccess { get; set; }
public int PublicWithLink_WriteAccess { get; set; }
public int PublicWithLink_DocsCount { get; set; }
public int InternalCollaborators_CollaboratorsCount { get; set; }
public int InternalCollaborators_Read { get; set; }
public int InternalCollaborators_Comment { get; set; }
public int InternalCollaborators_Write { get; set; }
public int InternalCollaborators_DocsCount { get; set; }
public int OutsideDomain_CollaboratorsCount { get; set; }
public int OutsideDomain_Read { get; set; }
public int OutsideDomain_Comment { get; set; }
public int OutsideDomain_Write { get; set; }
public int OutsideDomain_DocsCount { get; set; }
public int Domain_ReadAccess { get; set; }
public int Domain_CommentAccess { get; set; }
public int Domain_WriteAccess { get; set; }
public int Domain_DocsCount { get; set; }
public int DomainWithLink_ReadAccess { get; set; }
public int DomainWithLink_CommentAccess { get; set; }
public int DomainWithLink_WriteAccess { get; set; }
public int DomainWithLink_DocsCount { get; set; }
}
public class Domain
{
public int Users { get; set; }
public int Documents { get; set; }
}
您还可以使用 File.ReadAllText 方法:
string data = File.ReadAllText("D:\\jsontxt.json");
使用AttributeContainer
类型进行反序列化.
Use AttributeContainer
type for deserialization.
var container = DeserializeFromJson<AttributeContainer>(data);
这篇关于使用Json.net反序列化Json的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!