json字串和js对象之间互相转换

本文介绍了如何在JavaScript中将JSON字符串转换为JavaScript对象以及反之的操作。通过使用JSON.parse()和JSON.stringify()方法,开发者可以轻松地进行数据格式转换。
很简单:

json字串 ===>>js对象    :JSON.parse(jsonstr);
js对象     ===>>json字串:JSON.stringify(jsonObj) 


下面是扩展阅读--------------------------------------------------------------------------------------------

使用JSON时,最基本的工作就是JSON数据与JavaScript对象之间的互相转换。如浏览器
从服务器端获得JSON数据,然后转换为JavaScript对象在网页输出。


JSON: JavaScript Object Notation,JSON数据格式语法就是JavaScript创建对象的代码语法,所以可以使用JavaScript内建的eval()函数直接将JSON数据转换为JavaScript对象。如下:

1. JSON数据示例
{
"employees": [{
"firstName": "John",
"lastName": "Doe"
},
{
"firstName": "Anna",
"lastName": "Smith"
},
{
"firstName": "Peter",
"lastName": "Jones"
}]
}

JSON数据转换为JavaScript对象
var txt = '{ "employees" : [' +
'{ "firstName":"John" , "lastName":"Doe" },' +
'{ "firstName":"Anna" , "lastName":"Smith" },' +
'{ "firstName":"Peter" , "lastName":"Jones" } ]}';

var obj = eval ("(" + txt + ")");

使用eval()函数时,必须为传入的JSON数据参数添加括号'()',否则会报语法错误。但eval()的问题在于,除了可以解析JSON数据,也可以用于执行JavaScript脚本片段,这就会带来潜在的安全问题。
JSON提供了专门的JSON Parser来实现只用于解析JSON数据,不会执行scripts脚本,而且速度更快。如下:
obj = JSON.parse(txt);
在现在的浏览器中,如IE8, Firefox3.5, Chrome等以上版本,都提供了原生JSON解析器的支持,而且也是ECMAScript (JavaScript)的最新标准。因此,JSON Parser可以在JavaScript代码中是直接调用

JavaScript对象转换为JSON数据
var txt = JSON.stringify(obj);
使用JSON.strigify()函数,将Javascript对象转换为JSON文本数据。
分析參照以下mreditor.vue顯示相關病態變色的功能,新增在mreditor.vue上顯示相關草本變色的功能,草本資料fetch位置為MNTag/?format=json中,比對mrcase字段與mnname字段中的字串: mreditor.vue: <template> <div> <div> <div class="wangeditor"> <WangEditor v-model="content" @response="(msg) => content = msg" /> </div> <div class="right-panel"> <!-- 病态名称显示区域 --> <div class="dntag-display" v-if="currentDnTags.length"> <div class="field-name">相關病態:</div> <div class="dntag-value">{{ currentDnTags.join(';') }}</div> </div> <div class="dntag-display" v-else> <div class="field-name">相關病態:</div> <div class="dntag-value">無關聯病態名稱</div> </div> <mreditor1 ref="mreditor1" :showDnCrud="showDnCrud" @toggle-dn-crud="toggleDnCrud" /> </div> </div> <div class="tag-panel"> <!-- 病态编辑面板 --> <mrdncrud v-if="showDnCrud" @tag-updated="handleTagUpdated" /> </div> <div class="content-display" v-html="displayContent"></div> <footer class="sticky-footer"> <!-- 医案命名提交者输入框 --> <span class="form-input"> <input v-model="formData.mrname" placeholder="醫案命名" class="name-input" /> </span> <span class="form-input"> <input v-model="formData.mrposter" placeholder="提交者姓名" class="poster-input" /> </span> <span><button @click="toggleDisplayFormat">{{ displayButtonText }}</button></span> <span><button @click="resetAll" class="reset-btn">輸入新醫案</button></span> <span class="id-input"> <input v-model="fetchId" placeholder="輸入醫案ID" type="number" min="1" /> <button @click="fetchById">載入醫案</button> </span> <span v-if="submittedId"> <button @click="updateContent" class="update-btn">更新醫案</button> <button @click="deleteContent" class="delete-btn">刪除醫案</button> </span> <span v-else> <button @click="submitContent" class="submit-btn">提交醫案</button> </span> <span v-if="submittedId" class="submitted-id">醫案 ID: {{ submittedId }}</span> <span>醫案編輯器</span> </footer> </div> </template> <script> import WangEditor from './WangEditor.vue'; import mreditor1 from './mreditor1.vue'; import mrdncrud from './mrdncrud.vue'; import LZString from 'lz-string'; export default { name: 'mreditor', components: { WangEditor, mreditor1, mrdncrud }, data() { return { content: '', submittedId: null, fetchId: null, dnTags: [], showRawHtml: false, stateVersion: '1.0', autoSaveTimer: null, autoSaveInterval: 30000, currentDnTags: [], dnTagMap: {}, showDnCrud: false, formData: { mrname: "", mrposter: "" } }; }, computed: { highlightedContent() { if (!this.dnTags.length || !this.content) return this.content; const tempEl = document.createElement('div'); tempEl.innerHTML = this.content; const walker = document.createTreeWalker( tempEl, NodeFilter.SHOW_TEXT ); const nodes = []; while (walker.nextNode()) { nodes.push(walker.currentNode); } nodes.forEach(node => { let text = node.nodeValue; let newHtml = text; this.dnTags .slice() .sort((a, b) => b.length - a.length) .forEach(tag => { const regex = new RegExp( this.escapeRegExp(tag), 'g' ); newHtml = newHtml.replace( regex, `<span style="color: rgb(212, 107, 8); font-weight: bold;">${tag}</span>` ); }); if (newHtml !== text) { const span = document.createElement('span'); span.innerHTML = newHtml; node.parentNode.replaceChild(span, node); } }); return tempEl.innerHTML; }, displayContent() { if (this.showRawHtml) { return this.escapeHtml(this.content); } return this.highlightedContent; }, displayButtonText() { return this.showRawHtml ? '病態渲染格式' : '病態原始標籤'; } }, watch: { content: { handler() { this.debouncedSaveState(); }, deep: true } }, async mounted() { await this.fetchDNTags(); this.restoreState(); this.setupAutoSave(); }, activated() { console.log('醫案編輯器被激活'); this.restoreState(); this.setupAutoSave(); }, deactivated() { console.log('醫案編輯器被停用'); this.saveState(); this.clearAutoSave(); }, beforeDestroy() { this.clearAutoSave(); }, methods: { toggleDnCrud() { this.showDnCrud = !this.showDnCrud; if (this.showDnCrud) { document.querySelector('.tag-panel').style.display = 'block'; } else { document.querySelector('.tag-panel').style.display = 'none'; } }, handleTagUpdated() { console.log('病态标签已更新,刷新数据'); this.fetchDNTags(); }, escapeHtml(unsafe) { if (!unsafe) return ''; return unsafe .replace(/&/g, "&") .replace(/</g, "<") .replace(/>/g, ">") .replace(/"/g, """) .replace(/'/g, "'"); }, escapeRegExp(string) { return string.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); }, setupAutoSave() { this.clearAutoSave(); this.autoSaveTimer = setInterval(() => { if (this.content || this.submittedId) { console.log('自动保存状态...'); this.saveState(); } }, this.autoSaveInterval); }, clearAutoSave() { if (this.autoSaveTimer) { clearInterval(this.autoSaveTimer); this.autoSaveTimer = null; } }, debouncedSaveState() { this.clearAutoSave(); this.autoSaveTimer = setTimeout(() => { this.saveState(); this.setupAutoSave(); }, 2000); }, saveState() { const state = { version: this.stateVersion, content: this.content, submittedId: this.submittedId, formData: this.formData, showRawHtml: this.showRawHtml, currentDnTags: this.currentDnTags, timestamp: new Date().getTime() }; if (this.content.length > 2000) { try { state.compressed = true; state.content = LZString.compressToUTF16(this.content); } catch (e) { console.error('压缩失败,使用原始数据', e); state.compressed = false; } } sessionStorage.setItem('medicalRecordState', JSON.stringify(state)); }, restoreState() { const savedState = sessionStorage.getItem('medicalRecordState'); if (!savedState) return; try { const state = JSON.parse(savedState); if (state.version !== this.stateVersion) { console.warn('状态版本不匹配,跳过恢复'); return; } let content = state.content; if (state.compressed) { try { content = LZString.decompressFromUTF16(content); } catch (e) { console.error('解压失败,使用原始数据', e); } } this.content = content; this.submittedId = state.submittedId; this.showRawHtml = state.showRawHtml; this.currentDnTags = state.currentDnTags || []; this.formData = state.formData || { mrname: "", mrposter: "" }; console.log('医案状态已恢复'); } catch (e) { console.error('状态恢复失败', e); sessionStorage.removeItem('medicalRecordState'); } }, clearState() { sessionStorage.removeItem('medicalRecordState'); }, toggleDisplayFormat() { this.showRawHtml = !this.showRawHtml; this.saveState(); }, async fetchDNTags() { try { const response = await fetch('DNTag/?format=json'); const data = await response.json(); this.dnTags = data .map(item => item.dnname) .filter(name => name && name.trim().length > 0); this.dnTagMap = {}; data.forEach(item => { if (item.id && item.dnname) { this.dnTagMap[item.id] = item.dnname; } }); } catch (error) { console.error('获取标签失败:', error); alert('标签数据加载失败,高亮功能不可用'); } }, async fetchById() { if (!this.fetchId) { alert('請輸入有效的醫案ID'); return; } try { const response = await fetch(`MRInfo/${this.fetchId}/?format=json`); if (response.ok) { const data = await response.json(); this.formData.mrname = data.mrname || ''; this.formData.mrposter = data.mrposter || ''; this.content = data.mrcase || ''; this.submittedId = data.id; this.fetchId = null; if (data.dntag && Array.isArray(data.dntag)) { this.currentDnTags = data.dntag.map(id => this.dnTagMap[id] || `未知標籤(${id})` ); } else { this.currentDnTags = []; } await this.fetchDNTags(); this.saveState(); alert('醫案數據加載成功!'); } else if (response.status === 404) { alert('未找到該ID的醫案'); } else { throw new Error('獲取醫案失敗'); } } catch (error) { console.error('Error:', error); alert(`獲取醫案失敗: ${error.message}`); } }, async submitContent() { const postData = { mrcase: this.content, mrname: this.formData.mrname, mrposter: this.formData.mrposter }; try { const response = await fetch('MRInfo/?format=json', { method: 'POST', headers: { 'Content-Type': 'application/json', }, body: JSON.stringify(postData), }); if (response.ok) { const data = await response.json(); this.submittedId = data.id; if (data.dntag && Array.isArray(data.dntag)) { this.currentDnTags = data.dntag.map(id => this.dnTagMap[id] || `未知標籤(${id})` ); } else { this.currentDnTags = []; } this.saveState(); alert('醫案提交成功!'); } else { throw new Error('提交失败'); } } catch (error) { console.error('Error:', error); alert(`提交失败: ${error.message}`); } }, async updateContent() { if (!this.submittedId) return; const postData = { mrcase: this.content, mrname: this.formData.mrname, mrposter: this.formData.mrposter }; try { const response = await fetch(`MRInfo/${this.submittedId}/?format=json`, { method: 'PUT', headers: { 'Content-Type': 'application/json', }, body: JSON.stringify(postData), }); if (response.ok) { const data = await response.json(); if (data.dntag && Array.isArray(data.dntag)) { this.currentDnTags = data.dntag.map(id => this.dnTagMap[id] || `未知標籤(${id})` ); } this.saveState(); alert('醫案更新成功!'); } else { throw new Error('更新失败'); } } catch (error) { console.error('Error:', error); alert(`更新失败: ${error.message}`); } }, async deleteContent() { if (!this.submittedId) return; if (!confirm('確定要刪除這個醫案嗎?')) return; try { const response = await fetch(`MRInfo/${this.submittedId}/?format=json`, { method: 'DELETE', headers: { 'Content-Type': 'application/json', } }); if (response.ok) { this.clearState(); this.resetAll(); alert('醫案刪除成功!'); } else { throw new Error('刪除失败'); } } catch (error) { console.error('Error:', error); alert(`刪除失败: ${error.message}`); } }, resetAll() { this.content = ''; this.submittedId = null; this.fetchId = null; this.showRawHtml = false; this.currentDnTags = []; this.showDnCrud = false; this.formData = { mrname: "", mrposter: "" }; document.querySelector('.tag-panel').style.display = 'none'; } } }; </script> <style scoped> .wangeditor { flex: 1; padding: 10px; overflow-y: auto; } .right-panel { position: fixed; top: 55px; bottom: 45px; right: 0; width: 50%; background: lightblue; padding: 1px; z-index: 100; overflow-y: auto; } .dntag-display { display: flex; margin-top: 1px; padding: 10px; background: #f8f9fa; border: 1px solid #dee2e6; border-radius: 5px; box-shadow: 0 2px 4px rgba(0,0,0,0.05); } .field-name { font-weight: bold; min-width: 80px; color: #495057; font-size: 1rem; } .dntag-value { flex-grow: 1; color: #212529; font-weight: 500; padding-left: 10px; border-left: 2px solid #ffc107; } .tag-panel { position: fixed; top: 400px; bottom: 45px; right: 0; width: 30%; background: lightyellow; padding: 10px; z-index: 100; overflow-y: auto; display: none; } .content-display { position: fixed; top: 490px; left: 0; width: 50%; bottom: 45px; z-index: 999; background-color: white; overflow-y: auto; padding: 10px; border: 1px solid #eee; white-space: pre-wrap; } .sticky-footer { display: flex; justify-content: flex-start; align-items: center; position: fixed; bottom: 0; left: 0; width: 100%; background-color: #ffd800ff; z-index: 999; padding: 10px 20px; box-sizing: border-box; flex-wrap: wrap; } .sticky-footer > span { margin-left: 5px; display: flex; align-items: center; } .dncrud-btn { background-color: #9c27b0; color: white; border: none; padding: 2px 8px; border-radius: 4px; cursor: pointer; margin-left: 10px; } .dncrud-btn:hover { background-color: #7b1fa2; } .submitted-id { padding: 2px; background-color: #e2f0fd; color: #004085; border-radius: 4px; } .reset-btn { margin-left: 10px; padding: 2px; background-color: #dc3545; border: none; color: white; border-radius: 4px; cursor: pointer; } .reset-btn:hover { background-color: #c82333; } .id-input { display: flex; align-items: center; } .id-input input { width: 100px; padding: 2px 5px; margin-right: 5px; border: 1px solid #ccc; border-radius: 4px; } .submit-btn { background-color: #28a745; color: white; border: none; padding: 2px 8px; border-radius: 4px; cursor: pointer; } .update-btn { background-color: #007bff; color: white; border: none; padding: 2px 8px; border-radius: 4px; cursor: pointer; } .delete-btn { background-color: #dc3545; color: white; border: none; padding: 2px 8px; border-radius: 4px; cursor: pointer; } .submit-btn:hover { background-color: #218838; } .update-btn:hover { background-color: #0069d9; } .delete-btn:hover { background-color: #c82333; } .form-input { margin-right: 10px; } .name-input, .poster-input { padding: 5px 8px; border: 1px solid #ccc; border-radius: 4px; width: 150px; } </style>
最新发布
07-22
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值