Karthikeyan_kk
Karthikeyan_kk

Reputation: 85

How to read data from a text file in Jquery / JS to display it in html page?

{
  "symbol": "BPCL",
  "series": "EQ",
  "openPrice": "1,020.00",
  "highPrice": "1,041.95",
  "lowPrice": "1,016.55",
  "ltp": "1,040.55",
  "previousPrice": "1,013.45",
  "netPrice": "2.67",
  "tradedQuantity": "3,76,694",
  "turnoverInLakhs": "3,892.87",
  "lastCorpAnnouncementDate": "13-Jul-2016",
  "lastCorpAnnouncement": "Bonus 1:1"
} 

This is sample data given in gainers.txt file i want to parse these information and store it on separate variable eg var symbol should store all the symbols in txt file so that i can process with that variables later in my code.

Upvotes: 3

Views: 3093

Answers (2)

Fadhly Permata
Fadhly Permata

Reputation: 1686

You need to add a dataType:

$(document).ready(function() {
    $.ajax({
        url : "mytext.txt",
        dataType: "text",
        success : function (data) {
            $(".text").html(data);
            var jsonData = JSON.parse(data);
        }
    });
}); 

You can use jsonData as data json object format, or use data instead for text format.

Upvotes: 2

Lawrence Edel
Lawrence Edel

Reputation: 114

You should be able to do this using JSON parser

var a = '[{"symbol":"BPCL","series":"EQ","openPrice":"1,020.00","highPrice":"1,041.95","lowPrice":"1,016.55","ltp":"1,040.55","previousPrice":"1,013.45","netPrice":"2.67","tradedQuantity":"3,76,694","turnoverInLakhs":"3,892.87","lastCorpAnnouncementDate":"13-Jul-2016","lastCorpAnnouncement":"Bonus 1:1"}]';

var jsonData = JSON.parse(a);

alert(jsonData[0].symbol)
alert(jsonData[0].series)
alert(jsonData[0].openPrice)

etc

Upvotes: 0

Related Questions