Click here to Skip to main content
11,428,497 members (65,378 online)
Click here to Skip to main content

Tagged as

Converting PHP arrays to a C# Dictionary

, 21 Dec 2011 CPOL
Rate this:
Please Sign up or sign in to vote.
PHP array to C# Dictionary conversion.

Removed the previous code, it was a totally bad idea and awful code.  Now I'm passing the Json array to Silverlight using json_encode() and using Newtonsoft.Json library with the following simple function: 


  public Dictionary<string, object> Parse(string array)
            Dictionary<string, object> result = new Dictionary<string, object>();
            Newtonsoft.Json.Linq.JObject obj = (Newtonsoft.Json.Linq.JObject)JsonConvert.DeserializeObject(array);
            foreach (KeyValuePair<string, Newtonsoft.Json.Linq.JToken> kvp in obj)
                if (kvp.Value.ToString().Contains('{'))
                    result.Add(kvp.Key, Parse(kvp.Value.ToString().Replace("[", "").Replace("]", "")));
                    result.Add(kvp.Key, kvp.Value.ToString());
            return result;


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Technical Writer
No Biography provided

Comments and Discussions

QuestionUnable to cast object of type 'Newtonsoft.Json.Linq.JObject' to type 'Newtonsoft.json.linq.jarray' Pin
real-insanity24-Dec-11 12:24
memberreal-insanity24-Dec-11 12:24 
AnswerRe: Unable to cast object of type 'Newtonsoft.Json.Linq.JObject' to type 'Newtonsoft.json.linq.jarray' Pin
taralex3-Jan-12 3:31
membertaralex3-Jan-12 3:31 
GeneralRe: Unable to cast object of type 'Newtonsoft.Json.Linq.JObject' to type 'Newtonsoft.json.linq.jarray' Pin
real-insanity3-Jan-12 4:03
memberreal-insanity3-Jan-12 4:03 
QuestionCould't you just use jsonencode? Pin
RedAnthrax_30-Nov-11 13:10
memberRedAnthrax_30-Nov-11 13:10 
AnswerRe: Could't you just use jsonencode? Pin
taralex30-Nov-11 16:37
membertaralex30-Nov-11 16:37 
I looked at it briefly and got an impression, that I need to create my objects exactly reflecting the array, to which the jsonDesirializer will then deserialize the data. I also wasn't sure how it would handle multiple nesting of arrays and I wanted it to be a Dictionary, so that I could access PHP array members in the same way as in PHP, by the key. Maybe I just overlooked something, and the json .net library does it all for me, but I couldn't find any examples on the web, and figuring it out by myself would take more time than 2riting my own code.

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.150428.2 | Last Updated 21 Dec 2011
Article Copyright 2011 by taralex
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid