0

I have this JSON Data

{
"districtData": {
    "East Delhi": {
        "lastupdatedtime": ""
    },
    "South West Delhi": {
        "lastupdatedtime": ""
    },
    "West Delhi": {
        "lastupdatedtime": ""
    },
    "Delhi": {
        "lastupdatedtime": ""
    },
    "South Delhi": {
        "lastupdatedtime": ""
    },
    "North East Delhi": {
        "lastupdatedtime": ""
    },
    "North Delhi": {
        "lastupdatedtime": ""
    },
    "North West Delhi": {
        "lastupdatedtime": ""
    },
    "Unknown": {
        "lastupdatedtime": ""
    }
  }
} 

I want to print it in a loop and want to display like

East Delhi    lastupdatedtime 
North west   lastupdatedtime

and so on

<tr v-for="data in jsonData" :key="data.id"> 
   <td>{{ data}}</td>
</tr>

I tried but I am getting only lastupdatedtime. Can anyone help? Thanks in advance.

7
  • 1
    What is jsonData here? also the "JSON Data" you have shared has no id property then how are you assigning :key="data.id"? Commented Mar 28, 2020 at 7:48
  • i am fetching data using api call and storing it into jsonData object and :key="data.id" is necessary in vuejs otherwise it will not allow and through error Commented Mar 28, 2020 at 7:56
  • @LR yes but it must have id in the obj as well for this to work Commented Mar 28, 2020 at 7:57
  • But where is the id in jsonData object?? Commented Mar 28, 2020 at 7:57
  • ohh sorry but what to do ? any suggestion Commented Mar 28, 2020 at 8:01

4 Answers 4

1

There are two imporatnt fixes required in this code:

  1. Since there is no id in data you should use city name instead as it is unique
  2. Also, you should use the (value, key) in object approach for your problem.

An example of this code can be:

<div id="app">
  <ul>
    <li v-for="(cityData, city) in districtData" :key="city">
      {{city}} - {{cityData.lastupdatedtime}}
    </li>
  </ul>
</div> 

you can also use this pen for testing: https://codepen.io/abdullah-shabaz/pen/MWwZQYo

Sign up to request clarification or add additional context in comments.

1 Comment

Glad to be of help :)
1

you can also use

 <tr v-for="(data,index) in jsonData" :key="index"> 
  <td>{{ data.districtData}}</td>
  </tr>

3 Comments

thanks @ziakhan but it is displaying whole data as it is
if still you didnt understand then send me your proper array i will interpret for you, the way you want
Thanks you below code work for me thankyo for your time
0

Ok so for this to work you must have a proper formatted object.

{
"districtData": {
    "East Delhi": {
        "lastupdatedtime": "",
        "id": 1
    },
    "South West Delhi": {
        "lastupdatedtime": "",
        "id": 2
    },
    "West Delhi": {
        "lastupdatedtime": ""
         "id": 3
    },
    "Delhi": {
        "lastupdatedtime": "" 
        "id": 4
    },
    "South Delhi": {
        "lastupdatedtime": "" 
         "id": 5
    },
    "North East Delhi": {
        "lastupdatedtime": ""
       "id": 7
    }
}

and you key districtData will be use to loop through

4 Comments

thansk @Mr Khan, but i am fetching data from third party api i can't add id in that
you can still append this id to the response object.
how to append id any suggesstion?
Thankyou for your time i got output
0

If the jsonData is like this.

jsonData = {
    "districtData": {
        "East Delhi": {
            "lastupdatedtime": ""
        },
        "South West Delhi": {
            "lastupdatedtime": ""
        },
        "West Delhi": {
            "lastupdatedtime": ""
        },
        "Delhi": {
            "lastupdatedtime": ""
        },
        "South Delhi": {
            "lastupdatedtime": ""
        },
        "North East Delhi": {
            "lastupdatedtime": ""
        },
        "North Delhi": {
            "lastupdatedtime": ""
        },
        "North West Delhi": {
            "lastupdatedtime": ""
        },
        "Unknown": {
            "lastupdatedtime": ""
        }
    }
}

Please try below 2 methods.

It is to show all items in the object

<tr v-for="(data, index) in jsonData.districtData" :key="data.id"> 
    <td> {{ index }} </td>
    <template v-for="(item, index) in data" :key="index">
        <td> {{item}} </td>
    </template>
</tr>

It is to show lastupdatedtime.

<tr v-for="(data, index) in jsonData.districtData" :key="data.id"> 
    <td> {{ index }} </td>
    <td> {{data.lastupdatedtime}} </td>
</tr>

1 Comment

index is showing "districtData" and data is showing whole data as it is

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.