Gabain1993
Gabain1993

Reputation: 181

Storing object from json in localstorage

I am trying to store an object that I have as a Json string, to localstorage. Later I am going to retrieve the data, that's when I know the class.

However, if I store it as an object, all the variables get set to an empty object. And it throws an error when I try to read it later.

            LocalStore.SetItem(pKey, JsonConvert.DeserializeObject(pValue));

{Trip: [], PartialTrip: [], Route: [], Action: [], Driver: [], Vehicle: [], Trailer: [], StartTime: [],…}
Action: []
Driver: []
PartialTrip: []
PercentageAtDepot: []
PercentageInWarehouse: []
Route: []
StartTime: []
Trailer: []
Trip: []
Vehicle: []

If however I store it as a string, the quotes for the object gets escaped. Also later when retrieving the object, I get an error.

"{\u0022Trip\u0022:\u0022R000750\u0022,\u0022PartialTrip\u0022:10000,\u0022Route\u0022:30000,\u0022Action\u0022:\u0022LA\u0022,\u0022Driver\u0022:\u0022\u0022,\u0022Vehicle\u0022:\u0022TEST-1\u0022,\u0022Trailer\u0022:\u0022TEST-2\u0022,\u0022StartTime\u0022:\u00222021-11-02T08:00:00Z\u0022,\u0022PercentageAtDepot\u0022:100.0,\u0022PercentageInWarehouse\u0022:100.0}"

It seems the SetItem Generic Type because either Object or string. both of which go wrong. The response comes through SignalR and i can't use the dynamic type there.

Upvotes: 0

Views: 1014

Answers (1)

Gabain1993
Gabain1993

Reputation: 181

Of course after hours of searching I find the answer minutes after posting the question. Saving in local store has a version to save a raw string.

    LocalStore.SetItemAsString(pKey, pValue); // Save String Raw

The GetItem Function can then instantly parse this to the correct object.

Upvotes: 1

Related Questions