Đồng Vọng
Đồng Vọng

Reputation: 159

Read .txt file to json data jquery-javascript

I have code to auto generate .txt file, like that:

DataTable dt = ds.Tables[0];
string prdcontent = DataTableToJSONWithStringBuilder(dt);//this will generate json string
System.IO.File.WriteAllText(Server.MapPath("~/ContentPage/prdbank.txt"), prdcontent, Encoding.UTF8);  

OK. I have a .txt file with this content:

[{"matin":"1","matl":"11","tieude":"1 pha DT01P - DT01P/08 - DT01P/16","cloaisp":"0"},{"matin":"12","matl":"11","tieude":"3 pha DT03P","cloaisp":"0"},{"matin":"17","matl":"11","tieude":"1 pha DT01P60-RF","cloaisp":"1"},{"matin":"18","matl":"11","tieude":"3 pha DT03P05","cloaisp":"3"},{"matin":"19","matl":"11","tieude":"3 pha DT03P-RF","cloaisp":"3"},{"matin":"9","matl":"11","tieude":"1 pha DT01P80-RF - mẫu 2","cloaisp":"1"},{"matin":"10","matl":"11","tieude":"3 pha DT03P-RF - mẫu 2","cloaisp":"3"},{"matin":"4","matl":"11","tieude":"1 pha DT01P80-RF","cloaisp":"1"},{"matin":"5","matl":"11","tieude":"1 pha DT01P60-RF - mẫu 2","cloaisp":"1"},{"matin":"22","matl":"11","tieude":"1 pha DT01P-RF","cloaisp":"1"},{"matin":"23","matl":"11","tieude":"1 pha DT01P-RF mẫu 2","cloaisp":"1"},{"matin":"28","matl":"11","tieude":"1 pha DT01P80-RF mô-đun RF gắn ngoài","cloaisp":""},{"matin":"26","matl":"11","tieude":"1 pha DT01M10","cloaisp":""},{"matin":"27","matl":"11","tieude":"1 pha DT01M80","cloaisp":""},{"matin":"16","matl":"11","tieude":"3 pha DT03M01","cloaisp":"3"},{"matin":"20","matl":"11","tieude":"3 pha DT03M10","cloaisp":"3"},{"matin":"14","matl":"11","tieude":"3 pha DT03M05","cloaisp":"3"},{"matin":"15","matl":"11","tieude":"Bộ thu thập dữ liệu DCU","cloaisp":"2"},{"matin":"13","matl":"11","tieude":"Modem RMR TurboJet","cloaisp":"2"},{"matin":"21","matl":"11","tieude":"Hệ thống RFSPIDER","cloaisp":"2"},{"matin":"6","matl":"11","tieude":"Bộ định tuyến dữ liệu Router","cloaisp":"2"},{"matin":"7","matl":"11","tieude":"Bộ chuyển đổi RS232 / RS485","cloaisp":"2"},{"matin":"8","matl":"11","tieude":"Bộ đọc chỉ số công tơ qua sóng vô tuyến RF-EXT","cloaisp":"2"},{"matin":"11","matl":"11","tieude":"Hệ thống Giám sát HEMS","cloaisp":"2"},{"matin":"2","matl":"11","tieude":"Thiết bị Handheld Unit","cloaisp":"2"},{"matin":"3","matl":"11","tieude":"Hệ thống thu thập và quản lý dữ liệu đo đếm MDMS","cloaisp":"2"},{"matin":"25","matl":"11","tieude":"Bộ hiển thị chỉ số In Home display","cloaisp":""},{"matin":"29","matl":"11","tieude":"Bộ mở rộng ELSTER-RF","cloaisp":""},{"matin":"31","matl":"11","tieude":"T.bị giám sát chất lượng lưới điện","cloaisp":""},{"matin":"36","matl":"11","tieude":"Bộ mở rộng chức năng RF cho công tơ Landis","cloaisp":""},{"matin":"37","matl":"11","tieude":"Thiết bị cảnh báo sự cố trên lưới trung thế","cloaisp":""}]

Now, I wanna read this txt file and use result as json data, I'm using this function:

var jsontext_prdbank = (readTextFile('../ContentPage/prdbank.txt'));
var jsontext = JSON.parse(jsontext_prdbank);

With readtextfile function:

function readTextFile(file) {
var rawtext;
var rawFile = new XMLHttpRequest();
rawFile.open("GET", file, false);
rawFile.onreadystatechange = function () {
    if (rawFile.readyState === 4) {
        if (rawFile.status === 200 || rawFile.status == 0) {
            var allText = JSON.stringify(rawFile.responseText);
            rawtext = allText;
        }
    }
}
rawFile.send(null);
return rawtext;
}

I get jsontext like this: "[{"matin":"1.... (json string content)... cloaisp":""}]" but when I use
for (var i = 0; i < rowCount; i++) { var row = jsontext[i];}

, row just is "[". So that's mean JSON.parse(jsontext_prdbank) just return a string, not a json data. How can I get json data to use in this case?

Upvotes: 0

Views: 4466

Answers (3)

tolotra
tolotra

Reputation: 3270

Instead JSON.stringify, use JSON.parse to parse your file content to JavaScript object.

Upvotes: 1

The Alpha
The Alpha

Reputation: 146191

Because you've used the following code in your function:

var allText = JSON.stringify(rawFile.responseText);
rawtext = allText;

Just use the following instead:

// rawFile.responseText is already in string form
// so parse it into an object and assign it to allText
var allText = JSON.parse(rawFile.responseText);

Then you can use something like the following:

// jsontext_prdbank will get a parsed object, no need to parse it again
var jsontext_prdbank = (readTextFile('../ContentPage/prdbank.txt'));

Upvotes: 1

ild flue
ild flue

Reputation: 1361

Just remove JSON.stringify from your original code, responseText is a string

var jsontext_prdbank = (readTextFile('../ContentPage/prdbank.txt'));
var jsontext = JSON.parse(jsontext_prdbank);

function readTextFile(file) {
var rawtext;
var rawFile = new XMLHttpRequest();
rawFile.open("GET", file, false);
rawFile.onreadystatechange = function () {
    if (rawFile.readyState === 4) {
        if (rawFile.status === 200 || rawFile.status == 0) {
            rawtext = rawFile.responseText;
        }
    }
}
rawFile.send(null);
return rawtext;
}

To loop through the object from JSON.parse

    for(i in jsontext) { row = jsontext[i] }

Upvotes: 3

Related Questions