Click here to Skip to main content
Licence CPOL
First Posted 30 Nov 2011
Views 7,411
Bookmarked 4 times

Converting PHP arrays to a C# Dictionary

By | 21 Dec 2011 | Article
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)

About the Author

taralex

Technical Writer

Unknown

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
QuestionUnable to cast object of type 'Newtonsoft.Json.Linq.JObject' to type 'Newtonsoft.json.linq.jarray' Pinmemberreal-insanity11:24 24 Dec '11  
AnswerRe: Unable to cast object of type 'Newtonsoft.Json.Linq.JObject' to type 'Newtonsoft.json.linq.jarray' Pinmembertaralex2:31 3 Jan '12  
GeneralRe: Unable to cast object of type 'Newtonsoft.Json.Linq.JObject' to type 'Newtonsoft.json.linq.jarray' Pinmemberreal-insanity3:03 3 Jan '12  
QuestionCould't you just use jsonencode? PinmemberRedAnthrax_12:10 30 Nov '11  
AnswerRe: Could't you just use jsonencode? Pinmembertaralex15:37 30 Nov '11  

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.

Permalink | Advertise | Privacy | Mobile
Web02 | 2.5.120517.1 | Last Updated 21 Dec 2011
Article Copyright 2011 by taralex
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid