2013-12-10 145 views
0

解析這個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; 
    } 
+0

「我得到錯誤」給我們沒有任何信息。什麼*確切*發生?請閱讀http://tinyurl.com/so-list –

+0

什麼是錯誤? –

+0

其實我很自信如何顯示這個JSON我想從上面的文件中的所有值..我從參考http://stackoverflow.com/questions/17801607/json-parsing-in-c-sharp-using-json網絡。但他們使用事件結果,而不是我從streamreader傳遞字符串 – arun

回答

2

你必須根據JSON結構類型中進行建模。這些類應該工作:

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類型的反序列化。

var container = DeserializeFromJson<AttributeContainer>(data); 
+1

我寫了幾乎相同的東西,但你擊敗了我一拳。因爲我沒有添加任何東西...... + 1! –

+0

UfukHacıoğulları:嗨,謝謝你!它的工作。 – arun

0

您可以使用該網站來檢查你的JSON數據:

http://jsoneditoronline.org/index.html

我複製你的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 
    } 
} 
} 
+0

他在他的帖子超過代碼格式化塊的末尾。我不認爲這是他的問題。 –

+0

是的,那好吧..使用C#代碼解析它.. – arun

+0

@TimS。我有有效的Json文件一些參數錯過我只是現在更新..我想解析這些值使用C# – arun