0

I'm building a questionnaire builder where the answers to some questions can lead to follow-up questions. The next step is to serialize this data and process it into a SQL query. Even though I'm using jQuery, I don't think serializeArray() is complex enough to serialize the data in the way I want, which would be like:

Answer = {
    'text': String,
    'default': String,
    'img_path': String
}

Question = { 
    'type': String,
    'text': String,
    'followups': {
        Answer : [Question, Question, ...],
        Answer : [Question],
        Answer : []
    }
}

The problem I'm running into is that when I try to use an Answer object as a key in the 'followups' map--when I use JSON.stringify(), rather than stringifying the key, it is outputted as [object Object].

EDIT:

Stringifying the key is one way to keep the data, but then there are some nasty escapes, e.g.

"followups": {
    "{\"text\":\"asdfasdfa\"}": []
}

It'd be nice to fix this, but I'm open to any recommendations on a better way of serializing this form.

Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143
actaeon
  • 695
  • 1
  • 8
  • 17
  • 1
    a key in a JSON object can only be made out of strings. You can try adding another object of answers (using IDs or whatever) and use the Answer id to construct the followups – yoavmatchulsky Jul 23 '12 at 16:37

1 Answers1

1

You will probably need to modify your data structure slightly, something similar to this:

Question = { 
    'type': String,
    'text': String,
    'followups': [
        {
            answer : Answer,
            questions : [Question, Question, ...]
        },
        {
            answer : Answer,
            questions : [Question]
        },
        {
            answer : Answer,
            questions : []
        }
    ]
}
scottm
  • 27,829
  • 22
  • 107
  • 159
  • In this case, Question.followups should be an array, not an object – yoavmatchulsky Jul 23 '12 at 16:39
  • http://stackoverflow.com/questions/882727/is-a-way-that-use-var-to-create-json-object-in-key I interpreted that as being able to use any type of variable as a key; but thanks for your help! – actaeon Jul 23 '12 at 16:42