Sure! Below is a simple JSON viewer using Vue.js loaded via CDN. This implementation includes basic error handling and displays the formatted JSON in a collapsible tree structure.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>JSON Viewer</title>
<style>
body {
font-family: Arial, sans-serif;
margin: 20px;
}
.json-viewer {
margin-top: 20px;
}
.error {
color: red;
}
.collapsible {
cursor: pointer;
padding: 8px;
background-color: #f1f1f1;
border: none;
text-align: left;
outline: none;
font-size: 16px;
}
.active, .collapsible:hover {
background-color: #ccc;
}
.content {
padding: 0 18px;
display: none;
overflow: hidden;
background-color: #f9f9f9;
}
</style>
</head>
<body>
<div id="app">
<h1>JSON Viewer</h1>
<textarea v-model="jsonInput" placeholder="Paste or input JSON here..." rows="10" cols="50"></textarea>
<button @click="parseJson">Parse JSON</button>
<div v-if="error" class="error">{{ error }}</div>
<div v-if="parsedJson" class="json-viewer">
<div v-for="(value, key) in parsedJson" :key="key">
<button class="collapsible" @click="toggleCollapse(key)">{{ key }}</button>
<div class="content">
<json-viewer :json="value"></json-viewer>
</div>
</div>
</div>
</div>
<script src="https://unpkg.com/vue@3/dist/vue.global.js"></script>
<script>
const { createApp, defineComponent } = Vue;
const JsonViewer = defineComponent({
name: 'JsonViewer',
props: {
json: {
type: [Object, Array],
required: true
}
},
template: `
<div v-if="Array.isArray(json)">
<div v-for="(item, index) in json" :key="index">
<button class="collapsible" @click="toggleCollapse(index)">[{{ index }}]</button>
<div class="content">
<json-viewer :json="item"></json-viewer>
</div>
</div>
</div>
<div v-else>
<div v-for="(value, key) in json" :key="key">
<button class="collapsible" @click="toggleCollapse(key)">{{ key }}</button>
<div class="content">
<json-viewer :json="value"></json-viewer>
</div>
</div>
</div>
`,
methods: {
toggleCollapse(key) {
const content = this.$el.querySelector(`.content:nth-child(${parseInt(key) + 2})`);
if (content.style.display === "block") {
content.style.display = "none";
} else {
content.style.display = "block";
}
}
}
});
const App = defineComponent({
components: {
JsonViewer
},
data() {
return {
jsonInput: '',
parsedJson: null,
error: ''
};
},
methods: {
parseJson() {
try {
this.parsedJson = JSON.parse(this.jsonInput);
this.error = '';
} catch (e) {
this.parsedJson = null;
this.error = 'Invalid JSON';
}
}
}
});
const app = createApp(App);
app.component('json-viewer', JsonViewer);
app.mount('#app');
</script>
</body>
</html>
Explanation:
- HTML Structure: The HTML includes a textarea for JSON input, a button to parse the JSON, and a div to display the parsed JSON.
- CSS Styling: Basic styling for the collapsible elements and error messages.
- Vue.js Components:
- App Component: Manages the JSON input, parsing, and error handling.
- JsonViewer Component: Recursively renders the JSON in a collapsible tree structure.
- Script: Loads Vue.js via CDN and defines the components and app logic.
This implementation provides a minimal and functional JSON viewer with basic error handling and a collapsible tree structure for displaying the JSON.