Using localstorage - html

Got myself in a tricky situation. I'm using local storage to save values from a popup window, and then paste them into an input when focus returns to parent window.
But then something rather awkward takes place, when I try to store ';' separated values, is that I get only the 1st set, losing all the rest of the string. What makes it more bizarre is that after saving my value, I test by calling
alert('SELECTED : ' + localStorage.getItem('MyStr'));
the whole string is there... but on the script I retrieve this value, when i'm checking
alert(localStorage.getItem('MyStr'));
Only the 3rd set is there, i.e.: I store something like
abcdefg;123323;ffasfs;5445;iuiuifa;
but when I need to get it back, theres only
ffasfs
I could use some help then, I'm all new to this whole thing, and killing myself to get a website working.
Thanks in advance, sorry if my question looks stupid.

Store your values in localStorage as JSON strings. This may even help you build more complex objects for the future.
For now though... Just do:
localStorage.setItem("your key", JSON.stringify("abcdef;1234;whatever"));
This procedure will not only sanitize your input but also create oppertunity to store serialized objects in the future.
It's important to note that while JSON.stringify is pretty much supported everywhere... Not all browsers have it built in.
For those cases, check out json2.js.
Hope that helps.

Related

Unreadable Text in JSON

I've been working a bit with some files from Minecraft Dungeons, which were extracted using QuickBMS and made available here: https://minecraft.fandom.com/wiki/Minecraft_Wiki:Minecraft_Dungeons_game_files
In the "data" folder, there are a bunch of json-files, which I believe contain a list of textures associated with any given stage of the game. There is, however, a problem. When opened, it reads like any json-file, it has a bunch of names and values, but some of the values are not human-readable, they instead show up as a string of seemingly unrelated characters. Here an example:
"walkable-plane" : "eNpjYSEOMIMAOp+ZmQmND1fEjF2AiQldAJsWDEPRXUKkowkDAM/qA6o=",
Now, given that these are exclusively characters, and not error signs or something of the sorts, I'm assuming this is an encoding issue. Of course, I don't know for sure, Or I wouldn't be asking this in the first place, but the file as it appears in the text is UTF-8, and it obviously doesn't produce a usable result. So, if anyone knows what exactly this is, and how I could extract information from it, I'd be really thankful.

receive Excel data and turn into objects to format a JSON

I have this solution that helps me creating a Wizard to fill some data and turn into JSON, the problem now is that I have to receive a xlsx and turn specific data from it into JSON, not all the data but only the ones I want which are documented in the last link.
In this link: https://stackblitz.com/edit/xlsx-to-json I can access the excel data and turn into object (when I print document.getElementById('output').innerHTML = JSON.parse(dataString); it shows [object Object])
I want to implement this solution and automatically get the specified fields in the config.ts but can't get to work. For now, I have these in my HTML and app-component.ts
https://stackblitz.com/edit/angular-xbsxd9 (It's probably not compiling but it's to show the code only)
It wasn't quite clear what you were asking, but based on the assumption that what you are trying to do is:
Given the data in the spreadsheet that is uploaded
Use a config that holds the list of column names you want returned in the JSON when the user clicks to download
based on this, I've created a fork of your sample here -> Forked Stackbliz
what I've done is:
use the map operator on the array returned from the sheet_to_json method
Within the map, the process is looping through each key of the record (each key being a column in this case).
If a column in the row is defined in the propertymap file (config), then return it.
This approach strips out all columns you don't care about up front. so that by the time the user clicks to download the file, only the columns you want are returned. If you need to maintain the original columns, then you can move this logic somewhere more convenient for you.
I also augmented the property map a little to give you more granular control over how to format the data in the returned JSON. i.e. don't treat numbers as strings in the final output. you can use this as a template if it suites your needs for any additional formatting.
hope it helps.

Working with Powershell invoke-restmethod and json response

VERY newbie here with Powershell and JSON so may need a little extra help. Not a coder, fyi.
I'm working with Dell's API to retrieve warranty information on machines. I'd like to do it in Powershell if possible.
Here's my code so far -
$headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
$headers.Add("apikey", 'mykey')
$response = Invoke-RestMethod https://sandbox.api.dell.com/support/assetinfo/v4/getassetwarranty/servicecode -Headers $headers -contenttype 'application/json'
If I look at $response, here is the output -
AssetWarrantyResponse : {#{AssetHeaderData=; ProductHeaderData=; AssetEntitlementData=System.Object[]}}
InvalidFormatAssets : #{BadAssets=System.Object[]}
InvalidBILAssets : #{BadAssets=System.Object[]}
ExcessTags : #{BadAssets=System.Object[]}
AdditionalInformation :
Did some digging and found if I use the below code, I can start seeing actual data -
$response.AssetWarrantyResponse|fl
AssetHeaderData : #{BUID=11; ServiceTag=....
ProductHeaderData : #{SystemDescription=Op....
AssetEntitlementData : {#{StartDate=2017-11-2....
I see there is a command for convertfrom-json. Looks like this will take the response and make it a little easier to work with in Powershell. But I can't for the life of me make this work. I'm trying to pull out a few pieces of data from the AssetHeaderData and AssetEntitlementData above. I'm sure I could do some regex's, but if there's an easier way, I'm all for it!
Any help is appreciated. Thanks!
You're doing great! What you're seeing is PowerShell's system of displaying data on the screen. Since PowerShell tends to deal in complex objects, it's not just formatted string output you are dealing with like you might see from regular shell commands.
In this case, Invoke-RestMethod has automatically done the equivalent of ConvertFrom-Json for you, that is, it has taken the JSON returned from the API and created an object out of it.
The object has various properties, like .AssetWarrantyResponse, each of which could be a value or an array or another object.
So what you're seeing in the output, in your first dump of $response in your question, is the properties the response object has, and the values of those properties.
The values look funny because they are themselves objects, some of which contain arrays, and they are difficult to format all at once.
You've already figured out that you can reference the properties directly, for example by using $response.AssetWarrantyResponse.
When you look at that, you see more properties, which are also objects. I don't think it's going to be nested much more deeply based on the output you posted.
So to look at what's inside the properties you want, just keep digging away:
$response.AssetWarrantyResponse.AssetHeaderData
$response.AssetWarrantyResponse.AssetEntitlementData
You don't need regex for this at all, because the data is already broken up appropriately.
If there's something else you want to do with it, edit it into your question (or post a new one if warranted) and we can help with that too.
Update based on comment
Tab completion can be finicky. But as I look at your data again, I think I see why you saw what you saw, and tab completion was working correctly.
AssetWarrantyResponse is a single property, it will tab complete. Its value is an array. So trying to do .AssetWarrantyResponse.AssetHeaderData will not tab complete, because it's not a property of the object (an array).
If you did $response.AssetWarrantyResponse[0].AssetHeaderData then tab completion would work.
The reason it "works" when just typing it, is because of a change in PowerShell v3.
Before v3, if you have an array of objects and wanted to get an array of the .Thing property of each object, you would have to enumerate it yourself:
$things = foreach($item in $arr) {
$arr.Thing
}
v3 added a neat feature where you could just do $things = $arr.Thing and it handles it all for you. But since each item in the array might be completely different and have different properties, tab completion doesn't try to go into every one of them and read them and decide what to tab complete; you'd have to dereference yourself a single item and then tab completion will work as expected.
So even if you want to use the v3 goodness, you can use tab completion with a little extra typing:
$response.AssetWTAB[0].AssetHTAB
Then just go back and delete [0].
Just remember that if that property is an array, you should probably expect that .AssetWarrantyResponse.AssetHeaderData could also be an array, and so you must deal with that case appropriately in whatever comes next.
If you don't care about that situation and just want the first item, use [0] (or use [-1] if you only want the last one). Point is, if you want to assume it will only ever be a single element, make sure the code handles that assumption so it doesn't do strange things on an edge case.

How do I pass data between tvml views?

I need to pass data between views in my client-server app. For simple string value, I can put them as attributes on the target element and read the value when the select event is triggered on it. From there, I can pass this string value onto the next document pretty easily.
But the problem comes with much more complex data that's in JSON format. I tried doing JSON.stringify(myData) and putting this value in an attribute. But the compiler doesn't like the { in this attribute value.
I could probably try escaping all the different characters that the compiler has problems with. But I don't think that's a good idea.
Is there any way of implementing jQuery's .data() functionality in TVML and TVJS ? Or is there any other way that makes sending data between views a possibility ?
You can pass your data as URL parameters. Then in the new view, get them using Javascript.
EDIT: And I see in the comment above you came to a similar conclusion.
You could keep your data in a semi-global associative array. Store the key in an attribute on the element and use that to get your data structure.
Ex:
var globalData;
function onSelect(e){
var id=e.target.getAttribute("id");
var specificData=globalData[id];
}

Is there a Go Language equivalent to Perls' Dumper() method in Data::Dumper?

I've looked at the very similarly titled post (Is there a C equivalent to Perls' Dumper() method in Data::Dumper?), regarding a C equivalent to Data::Dumper::Dumper();. I have a similar question for the Go language.
I'm a Perl Zealot by trade, and am a progamming hobbyist, and make use of Data::Dumper and similar offspring literally hundreds of times a day. I've taken up learning Go, because it looks like a fun and interesting language, something that will get me out of the Perl rut I'm in, while opening my eyes to new ways of doing stuffz... One of the things I really want is something like:
fmt.Println(dump.Dumper(decoded_json))
to see the resulting data structure, like Data::Dumper would turn the JSON into an Array of Hashes. Seeing this in Go, will help me to understand how to construct and work with the data. Something like this would be considered a major lightbulb moment in my learning of Go.
Contrary to the statements made in the C counterpart post, I believe we can write this, and since I'll be passing Dumper to Println, after compilation what ever JSON string or XML page I pass in and decode. I should be able to see the result of the decoding, in a Dumper like state... So, does any more know of anything like this that exists? or maybe some pointers to getting something like this done?
Hi and welcome to go I'm former perl hacker myself.
As to your question the encoding/json package is probably the closest you will find to a go data pretty printer. I'm not sure you really need it though. One of the reasons Data::Dumper was awesome in perl is because many times you really didn't know the structure of the data you were consuming without visually inspecting it. With go though everything is a specific type and every specific type has a specific structure. If you want to know what the data will look like then you probably just need to look at it's definition.
Some other tools you should look at include:
fmt.Println("%#v", data) will print the data in go-syntax form.
fmt.Println("%T", data) will print the data's type in go-syntax
form.
More fmt format string options are documented here: http://golang.org/pkg/fmt/
I found a couple packages to help visualize data in Go.
My personal favourite - https://github.com/davecgh/go-spew
There's also - https://github.com/tonnerre/golang-pretty
I'm not familiar with Perl and Dumper, but from what I understand of your post and the related C post (and the very name of the function!), it outputs the content of the data structure.
You can do this using the %v verb of the fmt package. I assume your JSON data is decoded into a struct or a map. Using fmt.Printf("%v", json_obj) will output the values, while %+v will add field names (for a struct - no difference if its a map, %v will output both keys and values), and %#v will output type information too.