解析這個Json並獲取值Usign Json.NET.Help我找到解析的方法。 在此先感謝..反序列化Json使用Json.net
我使用Json.net Dll進行解析,下面是我的Json代碼和C#代碼來解析,但我得到錯誤,信任從此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;
}
「我得到錯誤」給我們沒有任何信息。什麼*確切*發生?請閱讀http://tinyurl.com/so-list –
什麼是錯誤? –
其實我很自信如何顯示這個JSON我想從上面的文件中的所有值..我從參考http://stackoverflow.com/questions/17801607/json-parsing-in-c-sharp-using-json網絡。但他們使用事件結果,而不是我從streamreader傳遞字符串 – arun