1

Hi I have a text file which consists of pair of latitude,longitudes ,These values need to be inserted into a java script at a particular line How to do that?

Also don't want the double quotes of "latlon" "lat" "lon"

My file content (PointData.txt):

[
 {"latlon":
     {"lon":77.57149375970222,"lat":12.906436894355084},
      "type":"s"
 },
{"latlon":  
    {"lon":77.56642974908229,"lat":12.8879050148954},
     "type":"s"
},
{"latlon":  
      {"lon":77.57149375970222,"lat":12.906436894355084},
       "type":"s"
},
{"latlon":
      {"lon":77.5722887689643,"lat":12.906389619827928},
       "type":"s"
 }
]

servlet:

   protected void doGet(HttpServletRequest request, HttpServletResponse  response) throws ServletException, IOException
    {
        response.setContentType("text/html");    
        PrintWriter out = response.getWriter();    

        System.out.println("HI");
        BufferedReader br = new BufferedReader(new FileReader("D:/Workspace/JAVA/VehicleDisplay/RPDATA.txt"));
        try {
            StringBuilder sb = new StringBuilder();
            String line = br.readLine();

            while (line != null)
            {
                sb.append(line);
                sb.append(System.lineSeparator());
                line = br.readLine();
            }
            String   value = sb.toString();
             System.out.println(value);


        } finally {
            br.close();
        }

    }

JavaScript:

 function main()
    {
      var result;

        $.ajax({
            url:'insertPos',
        //  data: {data : data},
            type:'get',             
             success:function(value)    
            {
                 result= value;
                 console.log(value);

            }

        });

    }
gariepy
  • 3,576
  • 6
  • 21
  • 34
Ramesh K
  • 41
  • 6
  • Your input is not valid JSON, it has a trailing `,`. I've removed that to format the JSON. –  Apr 13 '15 at 08:43
  • Ok ,But how to insert ?? – Ramesh K Apr 13 '15 at 08:46
  • possible duplicate of [How do I load the contents of a text file into a javascript variable?](http://stackoverflow.com/questions/196498/how-do-i-load-the-contents-of-a-text-file-into-a-javascript-variable) – Jordumus Apr 13 '15 at 08:49
  • Didn't get,Also I want to remove " " from the lat,lon & type – Ramesh K Apr 13 '15 at 08:57

2 Answers2

0
var request = new XMLHttpRequest();
request.open("GET", "path/to/PointData.txt", false);
request.send(null);
window.fileContent = JSON.parse(request.responseText));
John V
  • 875
  • 6
  • 12
0

Actually, you don't have to insert those right into current JS code. You can declare that data in variable inside of another JS file. Also, you can load it with AJAX.

If your data is updated rarely, the first way is simpler. Create an another JS file, include it in your HTML before current JS code, put your data there, then put variable declaration right before your data: var data = [your stuff]. Than you will be able to access it inside your main code as data. An AJAX solution will be more sophisticated, but it will require some learning from you.

Anton Melnikov
  • 1,048
  • 7
  • 21
  • Using AJAX ,Should I put data into another JS?? – Ramesh K Apr 13 '15 at 09:20
  • Sorry, man, I don't think I should write the whole thing for you for free. If you want to solve your problem - you've got to study a little. Here you can learn about AJAX https://developer.mozilla.org/en-US/docs/AJAX/Getting_Started – Anton Melnikov Apr 13 '15 at 09:32
  • I am not getting it..please help me – Ramesh K Apr 13 '15 at 10:54
  • function loadXMLDoc() { var textfile; if (window.XMLHttpRequest) { textfile = new XMLHttpRequest(); } textfile.onreadystatechange = function () { if (textfile.readyState == 4 && textfile.status == 200) { content = textfile.responseText; alert(content); } } textfile.open("GET", "D:/Workspace/JAVA/VehicleDisplay/RPDATA.txt", true); textfile.send(); } – Ramesh K Apr 13 '15 at 11:38
  • This is what i have tried and getting eroor here: D:/Workspace/JAVA/VehicleDisplay/RPDATA.txt – Ramesh K Apr 13 '15 at 11:39
  • Well, you need a web server to work with AJAX. Srsly, just put your data into variable, it's already a valid JS array of objects. – Anton Melnikov Apr 13 '15 at 12:15
  • check my edited code above ,I am able to read file from servlet ,Facing problem in passing it to ajax – Ramesh K Apr 13 '15 at 12:27