0

hey before i start with the main question i should state that the .json file is an external file in the same folder with .html files.I do not know if it helps but i am new to json.
My questions are 1) why jsfiddle throws throws errors at "papadopoulos_antonis": [{
"papadopoulos_stauros":=[{ "maria_anagnostou":= [{ 2) is it right to do <link src=.../> to read json file?

Thanks

//this is in a json external file.the file is in the same folder with other html files
   users = [

"papadopoulos_antonis": [{
fname: 'Παπαδόπουλος Αντώνης',
field1: 'Συντήρηση Αυτοκινήτου',
field2: 'Ορειβατικός εξοπλισμός',
field3: 'Μάθημα Καράτε',
field4: ''
}],

"papadopoulos_stauros":[{
fname: 'Παπαδόπουλος Σταύρος',
field1: 'Αναψυκτικά',
field2: 'Αλκοολούχα ποτά',
field3: 'Εξοδα καυσίμων',
field4: ''
}],

"maria_anagnostou": [{
fname: 'Αναγνώστου Μαρία',
field1: 'Διαφήμιση επιχείρησης',
field2: 'Τεχνικός επιχείρησης',
field3: 'Μηχανικός επιχείρησης',
feild4: ''
}] 
]


function jsonObjs(select) {
  
  var = JSONobject = JSON.parse(users);
  alert(users.papadopoulos_antonis[fname]);
  if (select == 1) {
    alert(users.papadopoulos_antonis[fname]);
  } else if (select == 2) {
    alert(users.papadopoulos_stauros[fname]);
  } else if (select == 3) {

  }
}
<link src="formMembers.json" type="text/javascript"/>


    <select id="selectCategory" onchange="jsonObjs(this)">
        <option>Επιλογή...</option>
        <option>Παπαδόπουλος Αντώνης</option>
        <option>Παπαδόπουλος Σταύρος</option>
        <option>Αναγνώστου Μαρία</option>
    </select>
nikolaosmparoutis
  • 440
  • 1
  • 6
  • 15
  • 2
    `fname: 'Αναγνώστου Μαρία'.` <<< `.` instead of `,` – Roko C. Buljan Feb 05 '15 at 04:13
  • *"this is in a json external file"* That is not valid JSON. It's simply a file containing JavaScript. *"is it right to do `` to read json file?"* No. JavaScript is included via ` – Felix Kling Feb 05 '15 at 04:14
  • I think you should learn more about arrays and objects in JavaScript: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Working_with_Objects – Felix Kling Feb 05 '15 at 04:23
  • then how if not link tag ? – nikolaosmparoutis Feb 05 '15 at 04:23
  • As I said you cannot include JSON files via HTML. However, you don't have JSON, you have JavaScript, so just use a ` – Felix Kling Feb 05 '15 at 04:27

1 Answers1

1

JSON.parse an invalid JSON will not work.

users = [ is an array and an array should look like:

users = ["val", "val", "val"]

instead you have:

users = [prop:"val", prop:"val", prop:"val"]

which should instead be an Object:

users = {prop:"val", prop:"val", prop:"val"} // Plain Object (not valid JSON)

Fix your .>>>, typo.
For a valid JSON use double-quotes:

{
    "papadopoulos_antonis": {
        "fname": "ΠαπαδόπουλοςΑντώνης",
        "field1": "ΣυντήρησηΑυτοκινήτου",
        "field2": "Ορειβατικόςεξοπλισμός",
        "field3": "ΜάθημαΚαράτε",
        "field4": ""
    },
    "papadopoulos_stauros": {
        "fname": "ΠαπαδόπουλοςΣταύρος",
        "field1": "Αναψυκτικά",
        "field2": "Αλκοολούχαποτά",
        "field3": "Εξοδακαυσίμων",
        "field4": ""
    },
    "maria_anagnostou": {
        "fname": "ΑναγνώστουΜαρία",
        "field1": "Διαφήμισηεπιχείρησης",
        "field2": "Τεχνικόςεπιχείρησης",
        "field3": "Μηχανικόςεπιχείρησης",
        "feild4": ""
    }
}

// Valid JSON http://jsonlint.com/

To get your JSON file see: Get JSON data from external URL and display it in a div as plain text

If you want to treat your "JSON" like an JS object than yes, you can call it using

<script src="myJson.js"></script>
<script>
    console.log(users); // Works
</script>
Community
  • 1
  • 1
Roko C. Buljan
  • 196,159
  • 39
  • 305
  • 313