Harshal
Harshal

Reputation: 3622

JSON.parse: unexpected character

i have an json array and i want to parse it in java-script but my jquery gives me following error:

JSON.parse: unexpected character 

The code in jquery is,on which the error comes:

// Attempt to parse using the native JSON parser first
if ( window.JSON && window.JSON.parse ) {//error comes on this line.
return window.JSON.parse( data );
} 

My code is:

success: function(msg){
                var test= JSON.parse(msg);
                var question=test.question;
        document.getElementById('question').value=question;

I am using the latest version of jquery

 <script src="http://code.jquery.com/jquery-latest.js"></script>

I am getting this in function(msg):

[Object { question=

"What is your religious affiliation?"

,  userQuestionCategoryId=

"1"

}]

Here is my Php code:

function getquest()
        {
             $id=$this->input->post('qid');
             $data=$this->adminsetting->getquestion($id);
             if(count($data)>0)
             //echo $data[0]->question; 
             echo json_encode($data);
             else
             echo "No Question In database";
        }

Thanks in advance.

Upvotes: 1

Views: 1436

Answers (2)

Harshal
Harshal

Reputation: 3622

Actually the json i s becomes alredy parsed so i have to just used the index,i am missing to use the index msg[0]. Thats why I am getting problem.

success: function(msg){



        document.getElementById('question').value=msg[0].question;

        document.getElementById('id').value=msg[0].userQuestionCategoryId;
        //alert(msg);
        } }); 

Upvotes: 1

Littlebobbydroptables
Littlebobbydroptables

Reputation: 3731

This:

{ question=

"What is your religious affiliation?"

,  userQuestionCategoryId=

"1"

}

Must be like this:

{"question":"What is your religious affiliation?",  "userQuestionCategoryId":"1"}

Upvotes: 1

Related Questions