Click here to Skip to main content
Click here to Skip to main content

Tagged as

Converting PHP arrays to a C# Dictionary

, 21 Dec 2011
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("]", "")));
                }
                else
                {
                    result.Add(kvp.Key, kvp.Value.ToString());
                }
            }
            return result;
        } 

License

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

Share

About the Author

taralex
Technical Writer
Unknown
No Biography provided

Comments and Discussions

 
QuestionCould't you just use jsonencode? PinmemberRedAnthrax_30-Nov-11 12:10 
AnswerRe: Could't you just use jsonencode? Pinmembertaralex30-Nov-11 15:37 

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 | Mobile
Web04 | 2.8.140827.1 | Last Updated 21 Dec 2011
Article Copyright 2011 by taralex
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid