-1

i have following problem:

i have an array with a lot of data retrieved from mssql and hand it over to a jsp, i will simplify it in a example:

("test","1","test2","2")

those are 4 fields. With split(",") i seperate the 4 fields, so that i can take each value and assign them to html-objects.

Now through a coincidence i found out, that if the Array is filled as follows:

("test","1","test,2","2")

where "test,2" is one text, the split command seperates the 4 values to 5 values. Is there any alternative or way so that the split command ignores the "," that are part of a string of a field?

Greetings,

Kevin

Update:

Sorry for the missunderstandings guys here i tried to simplify the code as far as i can:

<script>
   var jsArray = [];
   <%
    // Getting ArrayList from a request Attribute with a lot of data rows
    ArrayList arrayList = (ArrayList)request.getAttribute("DSList");
    for(int i=0;i<arrayList.size();i++){
        %>
        // Pushing each row to javascript array
        jsArray.push("<%= ((ArrayList)arrayList.get(i))%>");
        <%
    }%>
   // thats the split command that gets one line of the whole dataset
   Stringarray = jsArray[x].substr(1,jsArray[x].length-2).split(","); // where x is the current record
</script>

now i can simply call each filed with

Stringarray[n] //where n is the field number

thats how the code looks like, the problem now is that if in one of the Strings in any record line is a "," then the split command obviously would give back the wrong field count. I hope now it's more clear what i mean

11
  • We need to see your code to be able to help you. You might want to learn about Minimal, Complete, and Verifiable Examples. Commented Nov 23, 2017 at 14:14
  • 4
    Hi! Welcome to Stack Overflow. Some clarification: you say "array" but then "string" and wrap everything in (), I'm not sure what's going on. Commented Nov 23, 2017 at 14:14
  • 1
    Your backend data is screwed up if you can't differentiate between specific elements in a string. I mean, it looks like CSV, but even CSV has rules about how to treat data with commas, apostrophes etc. Commented Nov 23, 2017 at 14:16
  • 1
    It is better to return (do) well formatted data before send to client ! Commented Nov 23, 2017 at 14:17
  • 1
    yes it's javascript, but inside an <script> tag in a jsp Commented Nov 23, 2017 at 14:31

2 Answers 2

0

You can use non-greedy regex to filter out value inside "" and then remove "" from the words using array#map.

var string = '("test","1","test,2","2")';
var array = string.match(/"(.*?)"/g).map(function(item){
  return item.replace(/"/g,'');
});
console.log(array);

Sign up to request clarification or add additional context in comments.

3 Comments

i never worked with regex and the map-function, does that in any way compromise the loading time of the jsp via tomcat?
Most modern browser are fast, so if you don't have huge amount of data, it won't affect performance that much. Also, I don't have much experience with jsp.
ok still thank you, i will keep your method in mind, maybe i will find another way
0

Essentially, you have a backend data source and a front-end consumer. It just so happens that they reside on the same page, since you're using JSP to generate the page and the data. So treat it like a synchronous API, just embedded into the page.

How would you transmit data between and API and JavaScript? JSON.

So, stringify your result array into a JSON string and embed that into the JSP page, then JSON.parse() that in JavaScript and iterate as you would any other array.

Since I don't see your JSP code, I can't propose a more specific solution that what's linked here for creating JSON in JSP: how to add a list of string into json object in jsp? and Creating a json object in jsp and using it with JQuery

5 Comments

I'm sorry i thought i had made it clear what problem i have, i can't show you any code because the whole construct that manages the arrays is very complex. How can JSON help me get rid of the delimiter Problem?
@KevinBarz At the base you have an array somewhere in Java, say, ["test","1","test2","2"], make that into a JSON string: "[\"test\",\"1\",\"test2\",\"2\"]". In JavaScript, JSON.parse() that string and you'll get your array back in the page: Array [ "test", "1", "test2", "2" ]. JSON is a standard for data interchange, which is what you're doing, it's just that your endpoint is generating your page.
yeah but if i'm getting the value back from the JSON string then there is still the problem with the ","-delimiter when i want to split the array or am i missing something?
@KevinBarz No there will not be the errent , delimiter because you are not splitting anything. You are transforming your original Java array into a JSON array and then parsing that into a native JavaScript array. So the field that contains "test,2" will always contain "test,2". That's the whole point of JSON. You're treating it the same way you would an API endpoint, you're just skipping the AJAX part.
ok now i get the missunderstanding, i will update my first post maybe then it will be more clear

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.