0

I have a List of List that is converted into a string, passed through a GET function, and retrieved by AJAX.

Is it possible to get that string to behave like an array of arrays?

I've tried a random smattering of conversions, JSON.parse, using substring to remove the quotations, right now I'm looking at converting the variable prototype. The string is always treated as a string even tho it looks something like this on console.log(data)

data = [[27, 'category', 'item, 10, 11, 25, 30, 76], [28, 'category', 'item, 20, 25, 45, 60, 150]

//AJAX CALL
function clientDataAjax() {
        $.ajax({
            url : 'clientJobChartData.html',
            type: "get", //send it through get method
            data: { 
                clientName: 'Ver-a-fast'
            },
            success : function(data) {
               /*  $('#result').html(data); */
               clientUsageByJobByMonth = data.slice(1, data.length-1);
               //addDataset(clientUsageByJobByMonth);
               console.log(clientUsageByJobByMonth);
               console.log(JSON.parse(data));
            }
        });
    }


// GET METHOD
@RequestMapping(value = "/clientJobChartData", method = RequestMethod.GET)
    public @ResponseBody
        String pullClientData(String clientName){
        List<List<String>> clientData =  chartDao.clientAndJobUsage(clientName);
        String stringOfClientData = "'"+clientData+"'";
        return stringOfClientData;
    }

I was hoping that I could turn the string into an Array format and then be able to work with it as an array if there is a way to change the variable type.

Sorry if this is a silly question or dumb method. I'll be looking into creating JSON objects of the data I need to try and get around this. Thanks!

nick benz
  • 3
  • 2
  • 1
    You question is clearly about javscript. Why do you have java as a tag? They are unrelated languages. – WJS Jul 09 '19 at 18:13
  • I'm using Java and SpringBoot on the backend for the GET Method. If there is a way for spring or java to more effectively pass this information through without turning it into a string that would be great. – nick benz Jul 09 '19 at 18:16
  • 1
    Don’t return a `string` from the backend just return `List>` and use `JSON.parse()` in the front end. – StaticBeagle Jul 09 '19 at 18:24
  • Thanks StaticBeagle, I gave it a try and might be missing an appropriate dependency. The response I got back in the console is below. WARNING: Resolved [org.springframework.web.HttpMediaTypeNotAcceptableException: Could not find acceptable representation] – nick benz Jul 09 '19 at 18:33

1 Answers1

0

I am not too familiar with java but apparently the string conversion part is not right. Use a relevant library to convert the List to JSON and then on the client side you can simply use JSON.parse. Look at the accepted answer on this link

  • @nickbenz minor detail, if you’re using spring boot, you shouldn’t need GSON because it already has Jackson to deal with all the JSON stuff. So basically you have two JSON libraries in your project now. – StaticBeagle Jul 09 '19 at 18:44