Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I'm trying to parse the following json array

[
    {
        "email": "[email protected]",
        "timestamp": 1337197600,
        "smtp-id": "<[email protected]>",
        "event": "processed"
    },
    {
        "email": "[email protected]",
        "timestamp": 1337966815,
        "smtp-id": "<[email protected]>",
        "category": "newuser",
        "event": "clicked"
    },
    {
        "email": "[email protected]",
        "timestamp": 1337969592,
        "smtp-id": "<[email protected]>",
        "event": "processed"
    }
]

I've not really used json format before, so it's all a little new. I found I can parse a single element easily, i.e.

{
        "email": "[email protected]",
        "timestamp": 1337197600,
        "smtp-id": "<[email protected]>",
        "event": "processed"
}

dynamic stuff = JsonConvert.DeserializeObject(json);
Response.Write(string.Format("{0} = {1}<br />", "timestamp", stuff.timestamp)); 
//etc

But i'm struggling with how to get the individual elements into an array to loop through.

I though about splitting the sting on },{ but didn't have much luck with that. I imagine there's an easier way i'm missing.

Thank you.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
788 views
Welcome To Ask or Share your Answers For Others

1 Answer

Just deserialize the JSON as is and loop it...

dynamic stuff = JsonConvert.DeserializeObject(json);

foreach (var s in stuff) 
{
     Console.WriteLine(s.timestamp);
}

Fiddle: http://dotnetfiddle.net/0SthDp


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...