Click here to Skip to main content
15,867,488 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have JSON file in my application and I am tying to access the data in it using Java script.

NOTE: I am not looking for loading JSON file using Ajax call.

Json file Name: PhotoDatabase.json

Json content:

JavaScript
var photosdatabase =
{
  "photos":
  [
    {
      "photo_id": 27932,
      "photo_title": "Atardecer en Embalse",
      "photo_url": "http://www.panoramio.com/photo/27932",
      "photo_file_url": "http://mw2.google.com/mw-panoramio/photos/medium/27932.jpg",
      "longitude": -64.404945,
      "latitude": -32.202924,
    },....
  ]
}

HTML call of JSON file:

HTML
<script type="application/json" src="~/PhotosDatabase.json"></script>

Java Script access to json file:

JavaScript
pics = photosdatabase.photos;

The same works in local Java script files but not working in MVC.

In browser sources the JSON file is not loaded.

Should I add some permissions to make the JSON file load into browser?

What I have tried:

I tried to add this to my web config file
HTML
<staticContent>
    <mimeMap fileExtension=".json" mimeType="application/json"/>
</staticContent>


This didn't work. More over it messed up all and nothing loaded like content files and script files and loaded just text.
Posted
Updated 18-May-21 20:42pm

1 solution

That's not a json file, it's a javascript file. If that is literally what is in the file give it a js extension and treat it like any other js file.
 
Share this answer
 
Comments
Nithin B 15-Dec-16 10:05am    
Hi, thanks for the reply. It does work when I change extension to .js but if I remove var = from it it will be pure json right? Then will it work?
F-ES Sitecore 15-Dec-16 10:15am    
If you remove the var bit then yes it will be pure json, but the presence of the var stuff means it is javascript as that has to be parsed and executed.
Darshani Jayasekara 12-Feb-19 23:49pm    
I have a .json file without "var". but when I load it as,
<script type="application/json" src="manifest_1.json"></script>

Or
<link rel="manifest" href="manifest_1.json" charset="UTF-8"/>

It is not loading with "application/json" content type, it is loading with "Text/Html". I really need it to load as "application/json", since it is a manifest file. Is there a way to solve this issue?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900