Page 1 of 3

Stringify JSON parsing data and finding data saved in JSON

Posted: Fri Jul 25, 2014 2:32 am
by Alex Van Name

ok I have been reading about JSON and it seems like to make a JSON you are using STRINGIFY to build the object with each new "click".

I have also read that PARSE is used to break down the object that was created using stringify.

I guess now that this local storage variable is created, and it has this data packed, when I set a LABEL to this local storage variable no information is put into the LABEL.

var checkedItems = [];
var checkedItems = jQuery('[dsid="checkboxgroup"] input[type="checkbox"]:checked');
console.log("Checked items count = " + checkedItems.length);

//Iterate through checked items.
for(var i = 0; i < checkedItems.length; i++){
var checkedItem = jQuery(checkedItems);
var checkedText = checkedItem.closest("span").find("label").text();
var checkedValue = checkedItem.val();

//Just out this value to browser console. Please check out it to see results.
console.log("checkedItem[" + i + "] = " + checkedValue);

//Here you can use "checkedValue" JS variable as you need.
checkedItem.push({text: checkedText, value: checkedValue});
localStorage.setItem("checkedItemsLSV", JSON.stringify(checkedItem));
}
=======================================================
now I would like to see the data, so I should parse the data? Is that correct?

localStorage.setItem("checkedItemsLSV", JSON.stringify(checkedItem));
JSON.parse(checkedItem);

if the data is coming as check boxes in a single column, and all the values are entering a JSON, how do I get back my JSON values?


Stringify JSON parsing data and finding data saved in JSON

Posted: Fri Jul 25, 2014 1:53 pm
by Maryna Brodina

Hello!

We are very sorry for delay!

Please let me know if this helps http://stackoverflow.com/questions/52...


Stringify JSON parsing data and finding data saved in JSON

Posted: Fri Jul 25, 2014 2:47 pm
by Alex Van Name

Thank you Maryna,

I have been using this site to help me understand certain topics.

I am stuck with the Javascripting that is necessary for making my website functional and processing the data effectively.

I want to store a record of data , maybe something big like a Medical Report, and I am wondering if JSON is the best way to do this.


Stringify JSON parsing data and finding data saved in JSON

Posted: Mon Jul 28, 2014 12:39 am
by Yurii Orishchuk

Hi Alex,

pre

now I would like to see the data, so I should parse the data? Is that correct?

localStorage&#46;setItem("checkedItemsLSV", JSON&#46;stringify(checkedItem));
JSON&#46;parse(checkedItem);

/pre

To get stored in LSV string and parse it to the JS object(for futher use) you can use following code:

pre

&#47;&#47;Get LSV string&#46;
var storedItemsString = localStorage&#46;getItem("checkedItemsLSV");

&#47;&#47;This is your JS object&#46; You can access it like storedItems[0]&#46;text;
var storedItems = JSON&#46;parse(storedItemsString);

&#47;&#47;Here you access your storedItems object as you need&#46; For example:
for(var i = 0; i < storedItems&#46;length; i++){
alert(storedItems&#46;text);
alert(storedItems&#46;value);
};

/pre

Regards.


Stringify JSON parsing data and finding data saved in JSON

Posted: Mon Nov 24, 2014 7:08 pm
by M&M

hi,

I am unable to read using the above snippet. When I use alert box i can see the compete JSON string. Pls suggest. Below is my JSON string which is stored in LSV

{
"LeaveStatusResult": [
{
"ApplicationDate":"07-01-2013",
"LeaveType":"Annual Leave",
"FromDate":"31-01-2013",
"ToDate":"08-02-2013",
"Status":"Approved",
"PaidLeaves":"7",
"UnpaidLeaves":"0",
"Session":"",
"Remark":" - admin:",
"ID":641,
"SumLeaves":7
},
{
"ApplicationDate":"04-07-2013",
"LeaveType":"Annual Leave",
"FromDate":"12-08-2013",
"ToDate":"30-08-2013",
"Status":"Rejected",
"PaidLeaves":"15",
"UnpaidLeaves":"0",
"Session":"",
"Remark":" - admin:make changes as per ",
"ID":674,
"SumLeaves":15
}
]
}

Thanks,
M&M


Stringify JSON parsing data and finding data saved in JSON

Posted: Tue Nov 25, 2014 12:28 am
by Yurii Orishchuk

HI Murali,

Currently your problem is not clear.

Please show us screen shots or your implementation.

Regards.


Stringify JSON parsing data and finding data saved in JSON

Posted: Tue Nov 25, 2014 4:44 am
by M&M

hi,

I have stored the resulting JSON string (shown above) from a REST call to a local storage variable. Now how can I iterate and read all the keys and values so that I can populate either a List or Select Component.

Thanks,
M&M


Stringify JSON parsing data and finding data saved in JSON

Posted: Tue Nov 25, 2014 5:26 am
by Yurii Orishchuk

Murali,

Here is a code example for this case:

pre

&#47;&#47;Where "yourLSVName" is your lsv name where you stored your object in JSON format&#46;
var yourDataString = localStorage&#46;getItem("yourLSVName");

&#47;&#47;Convert JSON string into JS object&#46;
var yourData = JSON&#46;parse(yourDataString);

&#47;&#47;Get array from your response JS object&#46;
var = leaveStatusResult = yourData&#46;LeaveStatusResult;

console&#46;log("In below should be array:");
console&#46;log(leaveStatusResult);

for(var i = 0; i < leaveStatusResult&#46;length; i++){
var currentStatusResult = leaveStatusResult;
console&#46;log("leaveStatusResult[" + i + "] = " + currentStatusResult&#46;ApplicationDate);
};

/pre

Please read comments to understand how it works.

Regards.


Stringify JSON parsing data and finding data saved in JSON

Posted: Tue Nov 25, 2014 6:39 am
by M&M

hi Yurii,

cool thanks...it works. And really appreciate your snippet.

Btw there is a typo

var = leaveStatusResult = yourData.LeaveStatusResult;

gota be var leaveStatusResult = yourData.LeaveStatusResult; i guess :)

Thanks a ton,
M&M


Stringify JSON parsing data and finding data saved in JSON

Posted: Thu Apr 02, 2015 7:00 pm
by Joe7349603

Yurii,

Hopefully you can assist me with somewhat similar issue. I have tried to debug and tried different code but nothing is working.

I have a collection with column called keyword. My intention is to have a list of keywords that I store in array and later use to auto compete a search.

1) From the mapping below I am using debug label to populate my LSV called Traffic_offence_keyword and I also stringfy the code as follows:

codelocalStorage&#46;setItem("Traffic_offence_keyword", JSON&#46;stringify(value));/code

2) Next I have 2 services. Service 1 is called on page load to glab the keywords from the keyword column, and then populate LSV -apparently this is where things are failing.

3) on debug mode I can see the LSV is being populated as shown below however I can only see 1 keyword and I have 3 keywords stored. In addition I am getting an error: Uncaught SyntaxError: Unexpected end of input

code{"id":"551c5e47e4b07b1fd367a997","keyword":"drinking","createdAt":"2015-04-01 21:08:23&#46;208","_updatedAt":"2015-04-01 21:08:23&#46;208"} /code

4) On design view page load I have this code:
code
&#47;&#47;Get LSV string&#46;
var DataString = localStorage&#46;getItem("Traffic_offence_keyword");
&#47;&#47;Parse to JS object&#46;
var keywords = JSON&#46;parse(DataString);
&#47;&#47;Get array from response JS object&#46;
var results = keywords&#46;results;
&#47;&#47; access storedItems &#46;and show an alert
for(var i = 0; i < keywords&#46;length; i++){
&#47;&#47;this alert are for debugging
alert(keywords&#46;text);
alert(keywords&#46;value);
}
/code

4) The keyword_select menu you see there I am populating selectmenu from the DB ideally this whole exercise is to I don't have to use select menu.

Ask: Ideally all I need to accomplish in this exercise is: Populate the keywords into LSV, parse the sting to the right format and store that into an array that I can then use to run. How do I store the keywords in text format back to my array or another array ready for use
codeAppery("searchBox")&#46;autocomplete({source: Myarray}); /code
I feel like I am so close but I am not there. In addition, I added secure proxy which seems to have solved some other errors. Where I am going wrong?

Image Image