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>

5
  • 2
    fname: 'Αναγνώστου Μαρία'. <<< . instead of , Commented Feb 5, 2015 at 4: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 <link src=.../> to read json file?" No. JavaScript is included via <script> tags. (real) JSON files cannot be included via HTML tags. Commented Feb 5, 2015 at 4:14
  • I think you should learn more about arrays and objects in JavaScript: developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/… Commented Feb 5, 2015 at 4:23
  • then how if not link tag ? Commented Feb 5, 2015 at 4:23
  • As I said you cannot include JSON files via HTML. However, you don't have JSON, you have JavaScript, so just use a <script> element. Commented Feb 5, 2015 at 4:27

1 Answer 1

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>
5
  • It's still not JSON though, so JSON.parse(users) will throw and <link src=.../> won't work either. Commented Feb 5, 2015 at 4:22
  • @FelixKling well, for a valid JSON see above Commented Feb 5, 2015 at 4:27
  • to make it clear i want a json file so maybe the <link is not right Commented Feb 5, 2015 at 4:28
  • 1
    @MilsZ no, link is not right. Use AJAX or JSONP. I'll show you in a demo Commented Feb 5, 2015 at 4:29
  • the problem is i have only 4 hours to solve this and do other things until the project finishes and i do not have time to read so many things... But json is corrent but throws error at function jsonObjs(select) { ---expected '(End)' and instead show function Commented Feb 5, 2015 at 4:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.