2010-10-03 79 views
3

我有一個JSON格式的文件,其中包含個人用戶的記錄。有些用戶的評論字段停留在記錄中間。我只是想解析頂級項目(全名 contributorName 電子郵件)如何解析多態JSON數組?

使用Newtonsoft.JSON解析器,但我似乎無法得到它認識到單個對象。當我將整個字符串解析爲一個大對象時,我不知道如何迭代單個對象。

這就是我試圖做到這一點(財產屬性),但它不工作,如果他們是失序或有子屬性。我需要把它變成一個對象,而不是:

StreamReader re = File.OpenText("C:\\dropbox\\my dropbox\\clients\\towson\\english 317\\Ning Archive\\ning-members.json"); 
JsonTextReader reader = new JsonTextReader(re); 
string ct = ""; 

try 
{ 
    ct += "<table style='border:1px solid black'>"; 
    while (reader.Read()) 
    { 
     if (reader.TokenType == JsonToken.PropertyName) 
     { 
      if (reader.Value.ToString() == "fullName") 
      { 
       reader.Read(); 
       ct += "\r\n\r\n<tr><td>" + reader.Value + "</td>"; 
      } 
      if (reader.Value.ToString() == "contributorName") 
      { 
       reader.Read(); 
       ct += "<td>" + reader.Value + "</td></tr>"; 
      } 
      if (reader.Value.ToString() == "email") 
      { 
       reader.Read(); 
       ct += "<td>" + reader.Value + "</td>"; 
      } 
     } 
    }  
} 
catch { } 

ct+="</table>"; 
namesText.Text = ct; 

注意第一個記錄有我不關心一個註釋字段,但是在訂單的方式得到,當我試圖解析爲一個流。

[ 
{ 
    "createdDate": "2010-09-10T14:16:08.271Z", 
    "fullName": "Lisa Meloncon", 
    "gender": "f", 
    "country": "US", 
    "birthdate": "1969-05-14", 
    "comments": [ 
     { 
      "id": "6292914:Comment:272", 
      "contributorName": "0upfj0fd33932", 
      "description": "Thanks for joining! I'm working up a schedule for the students a bit late so I can assess some of their early writing (including the first assignment, a general evaluation of business writing skills) and determine a course that will address their needs. I plan to make liberal use of technology this semester, with a Screencasting assignment, some intermediate Word formatting drills, and various other activities.", 
      "createdDate": "2010-09-10T18:07:38.272Z" 
     } 
    ], 
    "email": "[email protected]", 
    "profilePhoto": "http://api.ning.com:80/files/251IcCtIBC3dGALHpG3ruYfg0Ip*EFJApPyMVGkiVArSUEvF*dK8A5grvPvl8eC7i7H0grhRH4pakLc9jSOww2GpU2OTq2nq/626617250.png?crop=1%3A1", 
    "level": "member", 
    "state": "active", 
    "contributorName": "2z42css3dgvoi" 
}, 
{ 
    "createdDate": "2010-09-08T02:57:00.225Z", 
    "fullName": "Robert Calabrese", 
    "gender": "m", 
    "location": "Baltimore, MD", 
    "country": "US", 
    "zip": "21284", 
    "birthdate": "1989-09-29", 
    "email": "[email protected]", 
    "profilePhoto": "http://api.ning.com:80/files/251IcCtIBC3dGALHpG3ruYfg0Ip*EFJApPyMVGkiVArSUEvF*dK8A5grvPvl8eC7i7H0grhRH4pakLc9jSOww2GpU2OTq2nq/626617250.png?crop=1%3A1", 
    "level": "member", 
    "state": "active", 
    "contributorName": "199ru4hzwc4n4" 
}, 
{ 
    "createdDate": "2010-09-04T22:36:51.158Z", 
    "fullName": "Regis Bamba", 
    "gender": "m", 
    "location": "Baltimore, MD", 
    "country": "US", 
    "zip": "21210", 
    "birthdate": "1986-09-29", 
    "email": "rbamba2xxx.edu", 
    "profilePhoto": "http://api.ning.com:80/files/251IcCtIBC3dGALHpG3ruYfg0Ip*EFJApPyMVGkiVArSUEvF*dK8A5grvPvl8eC7i7H0grhRH4pakLc9jSOww2GpU2OTq2nq/626617250.png?crop=1%3A1", 
    "level": "member", 
    "state": "active", 
    "contributorName": "2seadgzt89n6x" 
}, 
+0

請顯示您當前的解析代碼。 – 2010-10-03 01:51:06

回答

9

喜歡的東西:

JArray root = JArray.Load(reader); 
foreach(JObject o in root) 
{ 
    ct += "\r\n\r\n<tr><td>" + (string)o["fullName"] + "</td>"; 
    ct += "<td>" + (string)o["contributorName"] + "</td>"; 
    ct += "<td>" + (string)o["email"] + "</td>"; 
} 

我們使用顯式轉換得到一個字符串值從JToken返回由JObject.Item

但是,您應該考慮使用StringBuilder而不是串聯來獲得性能。

+0

優秀!那是LINQ能爲我做的嗎? – Caveatrob 2010-10-03 04:50:26

+0

@Caveatrob,LINQ to JSON受LINQ啓發,它可以用於LINQ to Objects。但是,它不是一個LINQ提供程序,我的答案不使用任何LINQ功能。 – 2010-10-03 04:53:52