Someone
Someone

Reputation: 10575

How do i use the jQuery.parseJSON properly

I have set of values coming from server side where iam trying to generate JSON and use it in my code

here is the statement now the problem is it is working perfectly if the value is fine with out single quotes and double quotes.

var stds = jQuery.parseJSON('<?php echo json_encode($this->emps); ?>');

  CASE 1:
  ABC:"E" DTSM  
  VCD:"E" DMST  

CASE 2:
  *****
  ABC:E DTSM  
  VCD:E DMST  

How can i still generate the JSON with case 1. iam getting JS error "exception thrown and not caught". This is due to malformed JSON string

Upvotes: 0

Views: 118

Answers (2)

use <?php echo json_encode($str); ?>

Upvotes: 1

biziclop
biziclop

Reputation: 14596

Because JSON is already a valid fragment of JavaScript, you could simply write

var stds = <?php echo json_encode( $this->emps ); ?>;

Upvotes: 5

Related Questions