0

How to make a JSON string into a javascript object. I am trying to convert the following string into JSON Object like this that is getting from the server

JSON String:

["{"title":"Admin Dhaka","href":"#0","dataAttrs":[],"data":["{\"title\":\"BNS HAJI MOHSIN\",\"href\":\"#0\",\"dataAttrs\":[\"{\\\"title\\\":\\\"id\\\",\\\"data\\\":\\\"51\\\"}\"]}","{\"title\":\"BNS ISSA KHAN\",\"href\":\"#1\",\"dataAttrs\":[\"{\\\"title\\\":\\\"id\\\",\\\"data\\\":\\\"1\\\"}\"]}","{\"title\":\"BNT KHADEM\",\"href\":\"#2\",\"dataAttrs\":[\"{\\\"title\\\":\\\"id\\\",\\\"data\\\":\\\"6\\\"}\"]}","{\"title\":\"BN DOCKYARD\",\"href\":\"#3\",\"dataAttrs\":[\"{\\\"title\\\":\\\"id\\\",\\\"data\\\":\\\"13\\\"}\"]}","{\"title\":\"BNT SEBAK\",\"href\":\"#4\",\"dataAttrs\":[\"{\\\"title\\\":\\\"id\\\",\\\"data\\\":\\\"7\\\"}\"]}","{\"title\":\"Naval Aviation\",\"href\":\"#5\",\"dataAttrs\":[\"{\\\"title\\\":\\\"id\\\",\\\"data\\\":\\\"89\\\"}\"]}","{\"title\":\"BNS SAIKAT\",\"href\":\"#6\",\"dataAttrs\":[\"{\\\"title\\\":\\\"id\\\",\\\"data\\\":\\\"40\\\"}\"]}","{\"title\":\"BNS Novojatra\",\"href\":\"#9\",\"dataAttrs\":[\"{\\\"title\\\":\\\"id\\\",\\\"data\\\":\\\"119\\\"}\"]}","{\"title\":\"BNS SHAH AMANAT\",\"href\":\"#10\",\"dataAttrs\":[\"{\\\"title\\\":\\\"id\\\",\\\"data\\\":\\\"11\\\"}\"]}"]}"] 

As an example, I have given one object in the above code actually the Array is a list of Objects like this

[obj1, obj2....] 

I tryed as follows:

var arr = '<?php echo !empty($treeView) ? $treeView : "[]"; ?>'; arr = JSON.parse(arr); console.log(arr); 

Getting the following error:

Uncaught SyntaxError: Unexpected token t in JSON at position 4 

PHP Code:

function ship_by_area_zone(){ $area_list = []; $ship_list = []; $zone = $this->utilities->findAllByAttributeWithOrderBy("bn_navyadminhierarchy", array("ADMIN_TYPE" => 1, "ACTIVE_STATUS" => 1), "CODE"); $area = $this->utilities->findAllByAttributeWithOrderBy("bn_navyadminhierarchy", array("ADMIN_TYPE" => 2, "ACTIVE_STATUS" => 1), "CODE"); // area wise ship foreach ($area as $key=>$value) { $row = $this->db->query("select * from bn_ship_establishment where AREA_ID = $value->ADMIN_ID and ACTIVE_STATUS = 1 order by CODE asc")->row(); if($row) { $dataAttrs = array(); $dataAttrs['title'] = 'id'; $dataAttrs['data'] = $row->SHIP_ESTABLISHMENTID; $dataAttrs = json_encode($dataAttrs); $ship_row = array(); $ship_row['title'] = $row->NAME; $ship_row['href'] = "#$key"; //"#1" $ship_row['dataAttrs'] = [$dataAttrs]; $ship_list[] = json_encode($ship_row); } } // zone wise area foreach ($zone as $key=>$value) { $row = $this->db->query("select * from bn_navyadminhierarchy where ACTIVE_STATUS = 1 and PARENT_ID = $value->ADMIN_ID order by CODE asc")->row(); if($row) { $area_row = array(); $area_row['title'] = $row->NAME; $area_row['href'] = "#$key"; $area_row['dataAttrs'] = []; $area_row['data'] = $ship_list; $area_list[] = json_encode($area_row); } } return json_encode($area_list); } 

Can anyone help me?

Thanks in advance!

8
  • ["{"title":" is malformed JSON. Try using json_encode instead Commented Feb 4, 2019 at 6:52
  • Try usng JSON.parse(), var obj = JSON.parse('{ "name":"John", "age":30, "city":"New York"}'); Commented Feb 4, 2019 at 6:53
  • Your JSON is not validated. Please validate the JSON from here: jsonlint.com and use JSON.parsey() to convert JSON to Object. Commented Feb 4, 2019 at 6:56
  • How you are generating this JSON? Commented Feb 4, 2019 at 6:59
  • I am making JSON Like this return json_encode([$area_list]); @SamiAhmedSiddiqui Commented Feb 4, 2019 at 7:02

3 Answers 3

2

You need to use json_encode in your PHP:

var arr = '<?php echo json_encode(!empty($treeView) ? $treeView : "[]"); ?>'; 
Sign up to request clarification or add additional context in comments.

Comments

1

First of all, the json string you have there is invalid. You can check free online to validate the json. Once you have a valid json, you can use JSON.parse() to convert it to JSONObject

Comments

1
// converting a simple javascript object to JSON object my_details = { "name" : "SL", "age " : "30" , "photo" : "imgMe.jpg" } my_details_in_json = JSON.stringify(my_details); // "{"name":"SL","age ":"30","photo":"imgMe.jpg"}" --> my_details_in_json 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.