Parse Json with objects containing a space

You must Login before you can answer or comment on any questions.

Hi I need to parse a json object which contains a space in it, how can I fetch it?

3 Answers

Accepted Answer

Post here the json, it HAS to work, I'm sure u're doing something funny there :)

— answered 8 months ago by Dan Tamas
answer permalink
7 Comments
  • http://pastie.org/4856659

    — commented 8 months ago by Julien Gattegno

  • I don't see any Soins Homme key, is the full json ?

    — commented 8 months ago by Dan Tamas

  • ],"Soin Homme": [ line 408

    — commented 8 months ago by Julien Gattegno

  • Show 4 more comments

This shouldn't be an issue, verify your json with an online tool.

— answered 8 months ago by Dan Tamas
answer permalink
2 Comments
  • my json is valid, i can access to categories without a space name but not the others containaing a space... I have something like this for one of the categories:

    json = JSON.parse(this.responseText);
        for (i = 0; i < json.Beaute[0].Soins.length; i++) {
            article = json.Beaute[0].Soins[i];
    But When i want to access to the 'Soins Homme' category, i cant get anything
    json = JSON.parse(this.responseText);
        for (i = 0; i < json.Beaute[0].Soins Homme.length; i++) {
            article = json.Beaute[0].Soins Homme[i];

    — commented 8 months ago by Julien Gattegno

  • Soins Homme should be in quotes. Otherwise, the JavaScript interpreter will think that these are two different variables.

    — commented 8 months ago by Christian Brousseau

Dan is right, the space shouldn't be an issue.

Although make sure that your json properties and values are between quotes.

For example, this won't work:
{
    prop: value with spaces
}

But this will:
{
    "prop": "value with spaces"
}

— answered 8 months ago by Christian Brousseau
answer permalink
3 Comments
  • thanks for your interest but I need something like this to work:

    {
        "prop name": "whatever value with spaces or not"
    }

    — commented 8 months ago by Julien Gattegno

  • Yes, I figured that much.

    Try the following sintax:

    json.Beaute[0].["Soins Homme"]

    — commented 8 months ago by Christian Brousseau

  • tried this:

    for (i = 0; i < json.Beaute[0].["Soins Homme"].length; i++) {
    this:
    for (i = 0; i < json.Beaute[0]."Soins Homme".length; i++) {
    and this:
    for (i = 0; i < json.Beaute[0]["Soins Homme"].length; i++) {

    but nor working

    — commented 8 months ago by Julien Gattegno

Your Answer

Think you can help? Login to answer this question!