-2

How do you access json in JavaScript?

In below image is the JSON that I've successfully send and I need it to retrieve in my client side. server

But when I print it in my console it looks like this. Do you guys have any idea what happened in here?

client

I tried it accessing by something like this in my Vue:

 <b-button @click="getDisease()">Search Disease</b-button>
          <b-form-input v-model="search"></b-form-input>
          <p>{{results}}</p>


import axios from "axios";

export default {
  data() {
    return {
      search: "",
      results: {}
    };
  },



      getDisease() {
          axios
            .get("http://localhost:9000/api/disease/" + this.search)
            .then(response => (this.results = response.data))
            .catch(error => alert(error));
          console.log(this.results);

          let myJson = JSON.stringify(this.results);
          console.log(myJson[0].ICD_C);
        },
Vin
  • 165
  • 1
  • 13
  • 1
    Can you post the JS code used to print? – Bruno Monteiro Sep 11 '19 at 04:01
  • From you image it seems like you are using observers. Provide more details for us to be able to help you.. like how are you making getting this response, how are you requesting etc.. – Manish Sep 11 '19 at 04:02
  • 1
    Possible duplicate of [How to parse JSON data with jQuery / JavaScript?](https://stackoverflow.com/questions/8951810/how-to-parse-json-data-with-jquery-javascript) – LF00 Sep 11 '19 at 04:03
  • @BrunoMonteiro I've updated the question – Vin Sep 11 '19 at 04:08
  • Are you using the Vue DevTools in Chrome? It may give better information and is the preferred method to debug Vue apps over using console.log or other methods. https://github.com/vuejs/vue-devtools – Gone Sep 11 '19 at 04:28

1 Answers1

0

JSON.parse(this.results); //should do the trick

  getDisease() {
      axios
        .get("http://localhost:9000/api/disease/" + this.search)
        .then(response => (this.results = response.data))
        .catch(error => alert(error));
      console.log(this.results);

      let myJson;
         try {
            myJson = JSON.parse(this.results);
         } catch(e) { console.log('err') }
      console.log(myJson[0].ICD_C);
    },
Dylan
  • 181
  • 2
  • 11
  • Got a new error though, "SyntaxError: Unexpected token o in JSON at position 1" – Vin Sep 11 '19 at 04:13
  • is this.results a valid string? I don't have access to your api so I have no clue what the value is... Try implementing a try catch and see if that works any better as I updated my post. – Dylan Sep 11 '19 at 04:16