0

My JSON object has the following structure :

{ key :
[
{keya1:value1,
keya2:value2,
keya3:[value31,value32...]
keya4:value4
},
{keyb1:value1,
keyb2:value2,
keyb3:[value31,value32...]
keyb4:value4
},
...
..
{keys1:value1,
keys2:value2,
keys3:[value31,value32...]
keys4:value4
}
]
}

I tried using a simple json.parse but it doesnt work. With such a structure how should i go about parsing so that i can safely iterate over each instances of key and display all keya-valuea,keyb-valueb.. pairs for values of key? Many thanks!

EDIT : I actually omitted quotes on purpose. The original JSON data HAS quotes around keys and values.Sorry i didnt mention it earlier.

2
  • Your actually better off getting this data structure to be properly json formatted. It will make it much easier on your part! Can you explain how the data is being generated? Commented Jan 16, 2013 at 8:32
  • Your json is invalid: Key names have to be quoted Commented Jan 16, 2013 at 8:34

3 Answers 3

2

Try using getJsonArray

JSONObject json = (JSONObject) JSONSerializer.toJSON(response);

JSONArray array= json.getJSONArray("key");

or getString("key"); where applicable

Sign up to request clarification or add additional context in comments.

3 Comments

I'm afraid this wouldn't work because the data is not formatted like JSON. Mainly the issue is that there is no double quotes around any of the strings.
Pretty sure the OP is using javascript
Also the OP asked for JavaScript.
0

Your JSON is invalid, you need the double quotes and comma after your array, when you have a correct JSON you could use JSON.parse

{ "key" :
        [{
          "keya1":"value1",
          "keya2":"value2",
          "keya3":["value31","value32..."],
          "keya4":"value4"
         },
         {
          "keyb1":"value1",
          "keyb2":"value2",
          "keyb3":["value31","value32..."],
          "keyb4":"value4"
         },
         {
          "keys1":"value1",
          "keys2":"value2",
          "keys3":["value31","value32..."],
          "keys4":"value4"
         }
    ]
}

Comments

0

try to quote each key and value:

var str = '{ key : \
    [ \
        {\
            keya1:value1, \
            keya2:value2, \
            keya3:[value31,value32], \
            keya4:value4 \
        }, \
        {\
            keyb1:value1, \
            keyb2:value2, \
            keyb3:[value31,value32], \
            keyb4:value4 \
        }, \
        {\
            keys1:value1, \
            keys2:value2, \
            keys3:[value31,value32], \
            keys4:value4 \
        } \
    ] \
}';

var pattern=/(\w+\d*)/ig;
var json=str.replace(pattern, '"$1"');
var parsedJSON=JSON.parse(json)
console.log(parsedJSON);

I hope this helps.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.