Vue CLI - Parsing nested data in component from local JSON

Ajaya Bajracharya

I want to render the following view from JSON with a Vue component:

article inside a section

JSON:

{
  "0": {
    "title": "Title0",
    "content": {
      "0": {
        "text": "few text here",
        "image": false
      }
    }
  },
  "1": {
    "title": "Title1",
    "content": {
      "0": {
        "text": "few text here",
        "image": false
      },
      "1": {
        "text": "few text here",
        "image": true,
        "imagePath": "../../Assets/images.sample.png"
      }
    }
  }
}

And to parse that data I have written the following Vue component:

<template>
  <div>
    <section v-for="(data, index) in jsonTitle" :key="index">
      <h5>{{data.title}}</h5>
      <article v-for="(data, index) in jsonTitle" :key="index">
        <h6>{{data.content[0].text}}</h6>
        <img v-if="data.content[0].image === true" v-bind:src="data.imagepath" alt="">
      </article>
    </section>
  </div>
</template>
<script>
  import json from "@/components/json/english.json";
  export default {
    name: "databox",
    data() {
      return {
        jsonTitle: json
      };
    }
  };
</script>

I am definitely missing something in this code. I only get the 1st data of the second title. Please provide a Vue CLI solution rather than Vue.js, as I am new and don't possess much knowledge yet.

Dan

First, any time you have a list of data in your JSON, use an array instead of an object with numbered indexes. For example:

const json = [
  {
    "title": "Title0",
    "content": [
      {
        "text": "few text here",
        "image": false
      }
    ]
  }
]
...

I changed the name jsonTitle to profiles, imagining that this is some profile data. It makes the template clearer to study, since you have two loops. Each loop has its own index used as a key. Here is how your component should look:

<template>
  <div>
    <section v-for="(profile, indexProfile) in profiles" :key="indexProfile">
      <h5>{{ profile.title }}</h5>
      <article v-for="(content, indexContent) in profile.content" :key="indexContent">
        <h6>{{ content.text }}</h6>
        <img v-if="content.image === true" :src="content.imagePath" alt="">
      </article>
    </section>
  </div>
</template>

<script>
import json from "@/components/json/english.json";
export default {
  name: "databox",
  data() {
    return {
      profiles: json
    };
  }
};
</script>

There was also a typo with imagepath instead of imagePath. Here is a demo

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

Get nested data from local JSON file

分類Dev

Parsing Nested Arrays Data

分類Dev

Vue JS: Accessing data from different component

分類Dev

Golang - parsing nested json values

分類Dev

Updating Functional Component Local State Using Data From Redux State

分類Dev

Parsing JSON data from CLOB field using PL/SQL

分類Dev

Get data from Firebase in Vue.js component

分類Dev

Table not showing the data from database in laravel vue component

分類Dev

filter data from nested array in vue.js

分類Dev

Golang find a value from a map of nested json data from MongoDB

分類Dev

how can I reference a method from the same component in the data of the component vue js

分類Dev

Error in parsing nested JSON list in flutter

分類Dev

Nested JSON Object Parsing in android using volley

分類Dev

Include local CSS files in a single vue component

分類Dev

parsing json data into listview in ListFragment

分類Dev

How to display data from a local JSON file by clicking the button?

分類Dev

Vue upload local json file

分類Dev

Extracting data from very nested JSON in Azure SQL

分類Dev

Accessing nested values in JSON data from Twitch API

分類Dev

Pass data to nested component in react.js

分類Dev

How to integrate Tailwindcss into a VUE CLI 3 project's web component?

分類Dev

How to check an specific user detail component using the user ID with vue-router from json file?

分類Dev

JSON parsing an array from PHP filled with SQL data returns [object Object]

分類Dev

vue.js - recursive components doesn't get updated when using nested array from raw json

分類Dev

Vue CLI dynamic import from project module

分類Dev

Get access to data of component from another component

分類Dev

parse json in a vue-treeselect component

分類Dev

android parsing json data using ION

分類Dev

Parsing JSON data using variables as keys

Related 関連記事

  1. 1

    Get nested data from local JSON file

  2. 2

    Parsing Nested Arrays Data

  3. 3

    Vue JS: Accessing data from different component

  4. 4

    Golang - parsing nested json values

  5. 5

    Updating Functional Component Local State Using Data From Redux State

  6. 6

    Parsing JSON data from CLOB field using PL/SQL

  7. 7

    Get data from Firebase in Vue.js component

  8. 8

    Table not showing the data from database in laravel vue component

  9. 9

    filter data from nested array in vue.js

  10. 10

    Golang find a value from a map of nested json data from MongoDB

  11. 11

    how can I reference a method from the same component in the data of the component vue js

  12. 12

    Error in parsing nested JSON list in flutter

  13. 13

    Nested JSON Object Parsing in android using volley

  14. 14

    Include local CSS files in a single vue component

  15. 15

    parsing json data into listview in ListFragment

  16. 16

    How to display data from a local JSON file by clicking the button?

  17. 17

    Vue upload local json file

  18. 18

    Extracting data from very nested JSON in Azure SQL

  19. 19

    Accessing nested values in JSON data from Twitch API

  20. 20

    Pass data to nested component in react.js

  21. 21

    How to integrate Tailwindcss into a VUE CLI 3 project's web component?

  22. 22

    How to check an specific user detail component using the user ID with vue-router from json file?

  23. 23

    JSON parsing an array from PHP filled with SQL data returns [object Object]

  24. 24

    vue.js - recursive components doesn't get updated when using nested array from raw json

  25. 25

    Vue CLI dynamic import from project module

  26. 26

    Get access to data of component from another component

  27. 27

    parse json in a vue-treeselect component

  28. 28

    android parsing json data using ION

  29. 29

    Parsing JSON data using variables as keys

ホットタグ

アーカイブ