1

I'm trying to grab the tweets from a super huge JSON file... all i want are the ones named "text"

JSON file looks like this:

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

EDIT: It's only grabbing the last text.. "text2".. why is it not grabbing everything as a list?

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);
5
  • please reopen.. i fixed it... Commented Jan 28, 2014 at 4:55
  • You didn't really fix anything. If anything, your update made your question invalid. Deserializing that JSON you originally posted works fine... you need to post the full exception message that you got if you want us to be able to help you. Commented Jan 28, 2014 at 5:10
  • .... and what is the result you get? Commented Jan 28, 2014 at 5:11
  • i'm not sure what you guys want from me? it catches an exception because of "A first chance exception of type 'Newtonsoft.Json.JsonSerializationException' occurred in Newtonsoft.Json.dll" Commented Jan 28, 2014 at 5:15
  • EDIT: okay it's not erroring anymore, but it's only grabbing the last "text".. test2.. it doesn't seem like there's anything else in the list for some reason.. anyone know why? Commented Jan 28, 2014 at 5:30

1 Answer 1

2

Your JSON is an array with just one object, which has multiple properties with the same name! And that's why you're only getting value from the last property.

It needs to be like that to be actually an array of objects, and each to have just one text property:

[
    {"text":"A nice cup of #coffee can speed your day up, and so can Firefox." },
    {"text":"test1" },
    {"text":"test2"}
]
Sign up to request clarification or add additional context in comments.

1 Comment

crap, i knew it was something, simple. will try and come back.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.