简体   繁体   中英

I'm getting data from my sql DB and How to remove Backslashes from JSON data?

This is what I'm getting from DB.

   "data": [
    {
      "gender": "Male",
      "faculty_data": "{\"email\": \"ab@gmail.com\", \"fname\": \"Subash\", \"lname\": \"Arumugam\", \"gender\": \"Male\", \"pin_code\": \"627861\", \"city_name\": \"1\", \"mobile_no\": \"9480\", \"color_name\": \"1\", \"state_name\": \"1\", \"blood_group\": \"1\", \"employee_id\": \"EMP002\", \"father_name\": \"Arum\", \"mother_name\": \"kani\", \"street_name\": \"77\", \"country_name\": \"1\", \"faculty_type\": \"1\", \"joining_date\": \"1995-12-12\", \"alt_mobile_no\": \"\", \"date_of_birth\": \"1995-06-07\", \"locality_name\": \"Alangulam\", \"mother_tongue\": \"1\", \"religion_name\": \"1\", \"faculty_awards\": \"Nilllllllllll\", \"marital_status\": \"Married\", \"total_years_exp\": \"10\", \"faculty_about_you\": \"----------\", \"official_email_id\": \"\", \"pri_edu_inst_name\": \"primary\", \"clge_pri_inst_name\": \"college\", \"high_edu_inst_name\": \"higher\", \"clge_high_inst_name\": \"college high\", \"faculty_achievement\": \"NOthing\", \"faculty_designation\": \"1\", \"faculty_qualification\": \"7\", \"last_worked_inst_name\": \"ASD\", \"faculty_monthly_salary\": \"26161914\", \"known_language_to_speak\": \"Sanga Tamil\", \"known_language_to_write\": \"Sanga Tamil\", \"pri_edu_inst_percentage\": \"85\", \"clge_pri_inst_percentage\": \"college\", \"high_edu_inst_percentage\": \"51\", \"last_worked_inst_address\": \"KCT\", \"clge_high_inst_percentage\": \"college high\", \"relieving_reason_last_inst\": \"Nill\"}",
      "caste_color_name": "White",
      "faculty_type_name": "Swipers",
      "designation_name": "Teacher Grade 1",
      "city_name": "Tirunelveli",
      "country_name": "India",
      "nationality": "Indian",
      "state_name": "Tamilnadu21",
      "language_name": "Sanga Tamil",
      "religion_name": "Hindu",
      "blood_group_name": "A pos",
      "username": "EMP002",
      "uname": "Subash Arumugam",
      "email": "ab@gmail.com",
      "faculty_qualification_type": "MCA"
    }
  ]
}

Laravel Function

 public function getFacultyAll() {
    $response['record'] = Faculties::join('caste_colors', 'caste_colors.id', '=', 'faculties.color_id')
                    ->join('faculty_types', 'faculty_types.id', '=', 'faculties.faculty_type_id')
                    ->join('faculty_designations', 'faculty_designations.id', '=', 'faculties.faculty_designation_id')
                    ->join('loc_cities', 'loc_cities.id', '=', 'faculties.city_id')
                    ->join('loc_countries', 'loc_countries.id', '=', 'faculties.country_id')
                    ->join('loc_states', 'loc_states.id', '=', 'faculties.state_id')
                    ->join('mother_tongues', 'mother_tongues.id', '=', 'faculties.mother_tongue_id')
                    ->join('religions', 'religions.id', '=', 'faculties.religon_id')
                    ->join('blood_group_types', 'blood_group_types.id', '=', 'faculties.blood_group_id')
                    ->join('users', 'users.id', '=', 'faculties.user_id')
                    ->join('faculty_qualifications', 'faculty_qualifications.id', '=', 'faculties.faculty_qualification_id')
                    ->select('faculties.gender', 'faculties.faculty_data', 'caste_colors.caste_color_name', 'faculty_types.faculty_type_name', 'faculty_designations.designation_name', 'loc_cities.city_name', 'loc_countries.country_name', 'loc_countries.nationality', 'loc_states.state_name', 'mother_tongues.language_name', 'religions.religion_name', 'blood_group_types.blood_group_name', 'users.username', 'users.uname', 'users.email', 'faculty_qualifications.faculty_qualification_type')
                    ->where('users.id', 8)->get();

    return json_encode(array("data" => $response['record']));
}

In the Faculty data field, I have stored in JSON values.

This is just a little demo showing what @Kaan said in his comment yesterday. I added a { before your received answer to turn it into a valid object defining string. You probably missed it when copying it. The following snippet works:

 var obj={ "data": [ { "gender": "Male", "faculty_data": "{\\"email\\": \\"ab@gmail.com\\", \\"fname\\": \\"Subash\\", \\"lname\\": \\"Arumugam\\", \\"gender\\": \\"Male\\", \\"pin_code\\": \\"627861\\", \\"city_name\\": \\"1\\", \\"mobile_no\\": \\"9480\\", \\"color_name\\": \\"1\\", \\"state_name\\": \\"1\\", \\"blood_group\\": \\"1\\", \\"employee_id\\": \\"EMP002\\", \\"father_name\\": \\"Arum\\", \\"mother_name\\": \\"kani\\", \\"street_name\\": \\"77\\", \\"country_name\\": \\"1\\", \\"faculty_type\\": \\"1\\", \\"joining_date\\": \\"1995-12-12\\", \\"alt_mobile_no\\": \\"\\", \\"date_of_birth\\": \\"1995-06-07\\", \\"locality_name\\": \\"Alangulam\\", \\"mother_tongue\\": \\"1\\", \\"religion_name\\": \\"1\\", \\"faculty_awards\\": \\"Nilllllllllll\\", \\"marital_status\\": \\"Married\\", \\"total_years_exp\\": \\"10\\", \\"faculty_about_you\\": \\"----------\\", \\"official_email_id\\": \\"\\", \\"pri_edu_inst_name\\": \\"primary\\", \\"clge_pri_inst_name\\": \\"college\\", \\"high_edu_inst_name\\": \\"higher\\", \\"clge_high_inst_name\\": \\"college high\\", \\"faculty_achievement\\": \\"NOthing\\", \\"faculty_designation\\": \\"1\\", \\"faculty_qualification\\": \\"7\\", \\"last_worked_inst_name\\": \\"ASD\\", \\"faculty_monthly_salary\\": \\"26161914\\", \\"known_language_to_speak\\": \\"Sanga Tamil\\", \\"known_language_to_write\\": \\"Sanga Tamil\\", \\"pri_edu_inst_percentage\\": \\"85\\", \\"clge_pri_inst_percentage\\": \\"college\\", \\"high_edu_inst_percentage\\": \\"51\\", \\"last_worked_inst_address\\": \\"KCT\\", \\"clge_high_inst_percentage\\": \\"college high\\", \\"relieving_reason_last_inst\\": \\"Nill\\"}", "caste_color_name": "White", "faculty_type_name": "Swipers", "designation_name": "Teacher Grade 1", "city_name": "Tirunelveli", "country_name": "India", "nationality": "Indian", "state_name": "Tamilnadu21", "language_name": "Sanga Tamil", "religion_name": "Hindu", "blood_group_name": "A pos", "username": "EMP002", "uname": "Subash Arumugam", "email": "ab@gmail.com", "faculty_qualification_type": "MCA" } ] }; obj.data[0].faculty_data=JSON.parse(obj.data[0].faculty_data); console.log(obj);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM