Получите Json внутри json и поместите его в текстовые поля, используя jquery - PullRequest
2 голосов
/ 27 января 2020

У меня есть такой вывод.

[{…}]
0:
id: 3
url_generation: "https://mail.google.com/mail/u/0/#sent"
status: "status one"
certificate: "[{"med_sbj_list":"Certificate1"},{"med_sbj_list":"Certificate2"},{"med_sbj_list":"Certificate3"},{"med_sbj_list":null},{"med_sbj_list":null},{"med_sbj_list":null}]"
name: "Doctor 1"
alphabet_name: "Doctor one"
image: "/doctor_photos/GSmdfr_Screenshot_20200114_102036.jpg"
image_caption: "this is an image caption"
image_alt: "image alt"
industry: "industry two"
conference: "[{"conf":"Conference1"},{"med_sbj_list":"Conference2"},{"conf":"Conference3"},{"conf":null},{"conf":null},{"conf":null}]"
birthday: "03-6-2018"
place_of_birth: "cebu city"
career_academic_back: "[{"from_year":"1991","from_month":"02","from_desc":"aaa","to_year":"1995","to_month":"06","to_desc":"bbb"}]"
career_work_exp: "[{"we_from_year":"1997","we_from_month":"05","we_from_desc":"ccc","we_to_year":"1997","we_to_month":"06","we_to_desc":"ddd"}]"
career_awards: "[{"from_year":"1998","from_month":"08","from_desc":"eee","to_year":"1999","to_month":"09","to_desc":"fff"}]"
sort_career: "1"
hospital_office: "industry one"
department: "[{"med_sbj_list":"industry one"},{"med_sbj_list":"industry one"},{"med_sbj_list":"industry one"},{"med_sbj_list":"industry one"},{"med_sbj_list":"industry one"},{"med_sbj_list":"industry one"}]"
doctor_comment: "this is a sample comment of the doctor."
created_at: "2020-01-22 03:50:28"
updated_at: "2020-01-22 03:50:28"
__proto__: Object
length: 1
__proto__: Array(0)

И я хочу получить значения certificate и conference типа json и поместить их в текстовые поля по отдельности. См. Рисунок ниже.

enter image description here

Это мой jquery код.

$.ajax({
                url: '/modal_edit_doctor/'+id,
                type: 'get',
                dataType: 'json',
                success: function(response){
                    console.log(response['data']);
                if(response == "success")
                  console.log(response['data']); 
                  $("#editdoctor").modal('show');
                  $("#url_generation").val(response['data'][0].url_generation);
                  $("#status").val(response['data'][0].status);
                  var objJSON = JSON.parse(response['data'][0].certificate);
                  console.log(objJSON);


                    $("#name").val(response['data'][0].name);
                    $("#alpha_name").val(response['data'][0].alphabet_name);
                    //image not included yet
                    $("#img_caption").val(response['data'][0].image_caption);
                    $("#img_alt").val(response['data'][0].image_alt);
                    //industry dropdown not included yet
                    //conference json not included yet
                    //birthday not included yet
                    $("#place_birth").val(response['data'][0].place_of_birth);
                    //the 3 careers not included yet
                    //checkbox not included yet
                    //hospital dropdown not included yet
                    //department json not included yet
                    $("#doc_comment").val(response['data'][0].doctor_comment);


                },
                    error: function(response){
                    alert('Error'+response);

                }

              });

Ответы [ 2 ]

1 голос
/ 27 января 2020

это будет так, я не очень хорош в jquery, но я напишу E JS

, используйте это, чтобы получить идею

var certificate1 = document.getElementbyId (' Certificate1 ')

certificate1.inner HTML = <% = response.certificate [0] .med_sbj_list%>

сначала необходимо получить строку сертификата, поскольку вы видите строку сертификата это массив, поэтому используйте его как массив для доступа к нему.

1 голос
/ 27 января 2020

L oop через него objJSON и вы получите доступ к данным. Я думаю, что вы хотите этого.

#textboxes is your text field id, maybe value or text you want to display.
$.each(objJSON, function(key,value){
   $("#textboxes").text(value.med_sbj_list);
});
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...