JSON satrini ajratishda sintaksik xato

Mening veb-sahifaimning ba'zi qismlari bilan JSON namunasi bor:

{"html": {"#data": "\n

Data

\n<div class="\"manufacturer-image\"">\n \n</div>\n
<form action="\"/manage/update-manufacturer-data/3\"" method="\"post\"">\n \n 
<div class="\"field\"">\n <div class="\"label\"">\n :\n 
</div>\n \n \n <div class="\"error\"">\n 
<input id="\"id_name\"" name="\"name\"" maxlength="50" type="\"text\"">\n 
  • Pole wymagane
\n </div>\n \n </div>\n\n 
<div class="\"field\"">\n <div class="\"label\"">\n :\n 
</div>\n \n \n <div>\n <input name="\"image\"" id="\"id_image\"" type="\"file\"">\n 
</div>\n \n </div>\n\n <div class="\"field\"">\n <div class="\"label\"">\n 
:\n </div>\n \n \n <div>\n 

\n </div>\n \n 
</div>\n \n <div class="\"buttons\"">\n <input class="\"ajax-save-button" button\"="" type="\"submit\"">\n 
</div>\n</form>"}}

Ushbu satr ajax chaqiruvi bilan qaytariladi va jQuery 1.6.1 xatolik chiqaradi:

SyntaxError: JSON.parse: expected ',' or '}' after property value in object

kodning keyingi qismida:

parseJSON: function( data ) {
    if ( typeof data !== "string" || !data ) {
        return null;
    }
   //Make sure leading/trailing whitespace is removed (IE can't handle it)
    data = jQuery.trim( data );

   //Attempt to parse using the native JSON parser first
    if ( window.JSON && window.JSON.parse ) {
        console.warn('data: ', data);
        var ret;
        try{
            ret = window.JSON.parse(data);
        } catch(e){
            ret = {};
            console.warn(e);
        }
        return ret;
        //return window.JSON.parse( data );
    }

Men bu yerda nima etishmayapman?


EDIT:

Men oldingi "json" (bu python ning simplejson lib bilan yaratilgan, shuning uchun qanday qilib bu har qanday joyda ishlaydigan bo'lishi mumkin, deb o'ylayman) va endi jsonlint ko'rsatadi, to'g'ri JSON bor. Holbuki, muammo bir xil. Yangi satr:

{"html": [{"#data": "\n

Data

\n<div class="manufacturer-image">\n    \n</div>\n<form action="/manage/update-manufacturer-data/4" method="post">\n        \n    <div class="field">\n        <div class="label">\n            :\n        </div>\n        \n        \n            <div class="error">\n                <input id="id_name" type="text" name="name" maxlength="50" />\n                
  • Pole wymagane
\n            </div>\n        \n    </div>\n\n    <div class="field">\n        <div class="label">\n            :\n        </div>\n        \n        \n            <div>\n                <input type="file" name="image" id="id_image" />\n            </div>\n        \n    </div>\n\n    <div class="field">\n        <div class="label">\n            :\n        </div>\n        \n        \n            <div>\n                

\n            </div>\n        \n    </div>\n  \n    <div class="buttons">\n        <input type="submit" class="ajax-save-button button" />\n    </div>\n</form>"}]}

EDIT2: OK, men JSOn mening orqa qismimni qoldirib, to'g'ri, ammo jimgina jQuery har bir "atrofida qo'shimcha tirnoq qo'shib turibdi" degani.

4
bilmayman, lekin uning ko'rinishidan json bekorga o'xshaydi ...
qo'shib qo'ydi muallif Rafay, manba

5 javoblar

storejson= getJSonObject("@ViewBag.JsonData");

function getJSonObject(value) {
    return $.parseJSON(value.replace(/"/ig, '"'));
}
10
qo'shib qo'ydi

< "##oneone" "/ kjAZ0"> emas, balki \" satrlardagi kod "\ .

Ushbu so'zda-JSON muallifiga murojaat qiling va unga mo'l-ko'l ning JSON Kutubxonalar mavjud uchun barchasi tillari va .

8
qo'shib qo'ydi
Yaxshi nuqta. Afsuski, muayyan tasdiqlash uchun havola yaratishning yo'lini topa olmayapman, shuning uchun JavaScript rejimini ideoda ishlatardim.
qo'shib qo'ydi muallif phihag, manba
Siz har doim foydalanishingiz mumkin: jsonlint.com , haqiqiy JSON tekshirish uchun. Bundan tashqari, yuqoridagi JSON tegishli emas.
qo'shib qo'ydi muallif Jamie R Rytlewski, manba

Men bu erda xato bo'lishi mumkin, lekin JSON ma'lumotlarining o'zi ishlamayotganligi sababli, deb o'ylayman. JSON parseri ehtimol JSON'ed mag'lubiyatga o'zgaruvchida joylashgan HTML-dagi ikkita tirnoqlarga chok qo'yadi. Menimcha, JSON ma'lumotlarini chiqarishdan oldin HTML satrini oldindan qayta ishlayotganingizda ishlaydi, shuning uchun siz ikki tirnoqni boshqa bir narsaga o'zgartirasiz. (va JSON ma'lumotlarini tahlil qilgandan keyin boshqa yo'lni tanlang)

Masalan, biroz tushunarli bo'lishi mumkin:

instead of this: {"html": { "#data": "<input name="somename" type="text"/>"} }

men buni ishlatishga harakat qilaman:

{"html": { "#data": "<input name="somename" type="text"/>"} }

Umid qilamanki ...

3
qo'shib qo'ydi
Bu men uchun ishladi. JSON.stringify (foo) .replace (/ \\ "/ g," \\\ "")
qo'shib qo'ydi muallif Rahul Gupta-Iwasaki, manba

JSON = Javascript ob'ekt nushasi, shuning uchun "# data" xususiyatining qiymati noto'g'ri mag'lubiyatdir.

You can validate your JSON here

2
qo'shib qo'ydi

Menimcha, bu sizning HTML kodingizdagi bir xil juft tirnoqlardan foydalansangiz, JSON o'zining mulk qiymatini o'ylaydi, barcha HTML uchun bitta tirnoqlardan foydalanmoqchi

0
qo'shib qo'ydi
Javascript UZB
Javascript UZB
99 ishtirokchilar

@js_uzb @vuejs_uz @react_uz @nodejs_uz @angular_uz @ngTashkent @yiiframework_uz @laravel_uz @linux_uzbek @python_uz @swift_uzb —————— @uzdevgroup @UzGeeksGroup ——— @UzDev_Jobs @jobs_uzb