0

I have a string in JavaScript I want to make some kind of JSON of key value pair. Here is string

 {
    "questionId": null,
    "articleId": null,
    "questionText": "eewrr",
    "pollType": null,
    "selectedOptionIds": [],
    "pollOptionList": [{
        "optionId": 0,
        "questionId": 0,
        "optionText": "werwer",
        "selectedByUser": "N",
        "createdDate": null,
        "modifiedDate": null
    }, {
        "optionId": 0,
        "questionId": 0,
        "optionText": "werwer",
        "selectedByUser": "N",
        "createdDate": null,
        "modifiedDate": null
    }, {
        "optionId": 0,
        "questionId": 0,
        "optionText": "werwer",
        "selectedByUser": "N",
        "createdDate": null,
        "modifiedDate": null
    }, {
        "optionId": 0,
        "questionId": 0,
        "optionText": "werwe",
        "selectedByUser": "N",
        "createdDate": null,
        "modifiedDate": null
    }, {
        "optionId": 0,
        "questionId": 0,
        "optionText": "rwer",
        "selectedByUser": "N",
        "createdDate": null,
        "modifiedDate": null
    }],
    "pollId": 37
}
1
  • 4
    parse this via JSON.parse() you will get object Commented Feb 3, 2017 at 7:11

5 Answers 5

1

You can use JSON.parse() please find below snippet for more information

var json = JSON.parse('{"questionId":null,"articleId":null,"questionText":"eewrr","pollType":null,"selectedOptionIds":[],"pollOptionList":[{"optionId":0,"questionId":0,"optionText":"werwer","selectedByUser":"N","createdDate":null,"modifiedDate":null},{"optionId":0,"questionId":0,"optionText":"werwer","selectedByUser":"N","createdDate":null,"modifiedDate":null},{"optionId":0,"questionId":0,"optionText":"werwer","selectedByUser":"N","createdDate":null,"modifiedDate":null},{"optionId":0,"questionId":0,"optionText":"werwe","selectedByUser":"N","createdDate":null,"modifiedDate":null},{"optionId":0,"questionId":0,"optionText":"rwer","selectedByUser":"N","createdDate":null,"modifiedDate":null}],"pollId":37}') 
console.log(json);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

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

Comments

0

for that purpose use

   JSON.parse()

Comments

0
var your_str = '{"questionId":null,"articleId":null,"questionText":"eewrr","pollType":null,"selectedOptionIds":[],"pollOptionList":[{"optionId":0,"questionId":0,"optionText":"werwer","selectedByUser":"N","createdDate":null,"modifiedDate":null},{"optionId":0,"questionId":0,"optionText":"werwer","selectedByUser":"N","createdDate":null,"modifiedDate":null},{"optionId":0,"questionId":0,"optionText":"werwer","selectedByUser":"N","createdDate":null,"modifiedDate":null},{"optionId":0,"questionId":0,"optionText":"werwe","selectedByUser":"N","createdDate":null,"modifiedDate":null},{"optionId":0,"questionId":0,"optionText":"rwer","selectedByUser":"N","createdDate":null,"modifiedDate":null}],"pollId":37}';
var obj = JSON.parse(your_str);
alert(obj);
//or
console.log(obj);

Here your_str is your json string.

Comments

0

You're going to want to parse it as an JSON Object So it's going to be:

JSON.parse('{"1": 1, "2": 2, "3": {"4": 4, "5": {"6": 6}}}', (key, value));

In your case it will be:

JSON.parse('{"questionId":null,"articleId":null,"questionText":"eewrr","pollType":null,"selectedOptionIds":[],"pollOptionList":[{"optionId":0,"questionId":0,"optionText":"werwer","selectedByUser":"N","createdDate":null,"modifiedDate":null},{"optionId":0,"questionId":0,"optionText":"werwer","selectedByUser":"N","createdDate":null,"modifiedDate":null},{"optionId":0,"questionId":0,"optionText":"werwer","selectedByUser":"N","createdDate":null,"modifiedDate":null},{"optionId":0,"questionId":0,"optionText":"werwe","selectedByUser":"N","createdDate":null,"modifiedDate":null},{"optionId":0,"questionId":0,"optionText":"rwer","selectedByUser":"N","createdDate":null,"modifiedDate":null}],"pollId":37}', key, value));

I hope I have provided you with enough information

Comments

0

use JSON.parse() for converting string to json and JSON.stringify() for converting json to string

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.