2014-01-28 28 views
1

我试图从一个超级巨大的JSON文件抢鸣叫......我想要被命名为一“文”反序列化JSON数组作为列表

JSON文件是这样的:

[{"text":"A nice cup of #coffee can speed your day up, and so can Firefox.", "text":"test1", 
"text":"test2"}] 

编辑:它只抓取最后的文本..“text2”..为什么它不抓取所有的列表?

public class JSONClasses 
    { 
     public class SingleTweet 
     { 
      [JsonProperty("text")] 
      public string text { get; set; } 
     } 
    } 

    public class JSONFunctions 
    { 
     //public static JSONRoot jsonFile = new JSONRoot(); 
     public static List<JSONClasses.SingleTweet> TweetList = new List<JSONClasses.SingleTweet>(); 

     public static bool Deserialize(string path) 
     {    
      try 
      { 
       var filePath = File.OpenText(path); 
       TweetList = JsonConvert.DeserializeObject<List<JSONClasses.SingleTweet>>(filePath.ReadToEnd()); 
       filePath.Close(); 
       return true; 
      } 
      catch (Exception) 
      { 
       Console.WriteLine("Could not Deserialize: " + path); 
       return false; 
      } 
     } 
    } 

//test to see if it works: 
JSONFunctions.Deserialize(AppOptions.JSONTwitterFilePath); 

foreach (JSONClasses.SingleTweet temp in JSONFunctions.TweetList) 
       Console.WriteLine(temp); 
+0

请重新打开..我固定它... – user1189352

+0

你没有真正解决任何东西。如果有的话,你的更新使你的问题无效。反序列化您最初发布的JSON工作正常......如果您希望我们能够为您提供帮助,您需要发布_full_例外消息。 –

+0

....你得到的结果是什么? – iandotkelly

回答

2

你的JSON是一个只有一个对象的数组,它有多个属性,它们有相同的名字!这就是为什么你只从最后一个属性获得价值。

它需要像要被实际对象的数组,每到只有一个text属性:

[ 
    {"text":"A nice cup of #coffee can speed your day up, and so can Firefox." }, 
    {"text":"test1" }, 
    {"text":"test2"} 
] 
+0

废话,我知道这是一件简单的事情。会试着回来。 – user1189352

+0

fml,它的工作.. ty – user1189352