tim foran
Posts: 0
Joined: Sun Mar 30, 2014 6:32 am

Storing multiple JSON arrays from REST response in localStorage

I have an app that is based around a search function. The results returned are relatively complex, and each result includes a JSON array with data unique to the search just performed.

I need to find a way to store the JSON array from each result so that it can be used on a detail page that is access by clicking the specific result. What I am thinking is to use JSON.stringify(), but I am not sure how to set that in the data mapping so that each result's array is stored properly in local storage.

Can somebody guide me on how to do that? Thanks!

tim foran
Posts: 0
Joined: Sun Mar 30, 2014 6:32 am

Storing multiple JSON arrays from REST response in localStorage

Hi Katya,

Thanks for the quick reply. I will look into this.

How do I use the code mentioned in that conversation to pull directly from the REST response and into local storage? Where/when do I run that?

I have an array for each result, and depending on the result selected to be viewed in Details, I need to be sure I choose the correct array.

Kateryna Grynko
Posts: 0
Joined: Thu Nov 15, 2012 9:13 am

Storing multiple JSON arrays from REST response in localStorage

Tim,

You can add a service Success event handler. Access the service response using variable "data". You can save it all:prelocalStorage.setItem("myResponse",JSON.stringify(data));/pre

tim foran
Posts: 0
Joined: Sun Mar 30, 2014 6:32 am

Storing multiple JSON arrays from REST response in localStorage

Hi Katya,

That makes sense. So I save the entire response to localStorage, and then access it on the next page with getItem("myResponse").

I can then convert it back to JSON, then just search the entire response for the object that has the same ID as the result I'm looking at. Once I have that, I can return the array needed for that result.

Am I understanding this correctly?

Thanks!

Kateryna Grynko
Posts: 0
Joined: Thu Nov 15, 2012 9:13 am

Storing multiple JSON arrays from REST response in localStorage

Hi Tim,

Yes, you're right.

tim foran
Posts: 0
Joined: Sun Mar 30, 2014 6:32 am

Storing multiple JSON arrays from REST response in localStorage

Hi Katya,

I wrote the following code, but am having trouble getting it all to execute. I am basically extracting the array I get from localStorage and parsing it back to JSON, and then using an array from that to create a chart. It is telling me that my function 'length' is undefined:

//retrieve initial results response from localstorage
var search_results = localStorage.getItem('results_response');

//convert retrieved string back to JSON array
var search_results_object = JSON.parse(search_results); //search_results_objects is now a JSON array

var results = search_results_object.results; //pull out results array

//get venue_id of detail page from local storage
var ven_id = localStorage.getItem('venue_id');

//search JSON response for venue ID
var chartDataResults = [];
var searchField = "id";
var searchVal = ven_id;
for (var i=0 ; i < results.length ; i++)
{
if (results[searchField] == searchVal) {
chartDataResults.push(results);
}
}

var chartDataInput = chartDataResults.essence_tags;
//this all works

var chartData = chartDataInput;

var chart = new AmCharts.AmSerialChart();
chart.dataProvider = chartData;
chart.categoryField = "tag";
chart.rotate = true;

var graph = new AmCharts.AmGraph();
graph.valueField = "relevance";
graph.type = "column";
chart.addGraph(graph);

var categoryAxis = chart.categoryAxis;
categoryAxis.autoGridCount = false;
categoryAxis.gridCount = chartData.length;
categoryAxis.gridPosition = "start";
categoryAxis.labelRotation = 90;
graph.fillAlphas = 0.8;
chart.write('chartdiv');

Any thoughts? Thanks!

Maryna Brodina
Posts: 0
Joined: Thu Apr 05, 2012 7:27 am

Storing multiple JSON arrays from REST response in localStorage

Hello!

Please try to debug app. Most likely you expect array, but there is not array. Set breakpoints in code and go through step by step.

Return to “Issues”