Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free.

I've been reading a ton on this and can't seem to find a solution that works for me. I am building a drag and drop menu system and saving the structure to the db as JSON.

I have a hidden field as part of a form that submits to the db. This hidden field has the full JSON string in it.

When I update a particular node/menu item, I want to search the value of the hidden text field, find the 'section' of JSON I want to update and replace it with the new values.

What is the best solution for this? grep? replaceWith?

Example before and after JSON

// This is the full json string
[{"title":"Cool link","link":"link","cssclass":"","cssid":"","id":"1399209929525"},{"title":"New link","link":"new-link.html","cssclass":"","cssid":"","id":"1399209790202"},{"title":"Another link","link":"cool","cssclass":"","cssid":"","id":"1399209834496"}]

// This is the updated section 
[{"title":"Another link changed","link":"cool","cssclass":"","cssid":"","id":"1399209834496"}]

So I have the updated section with a unique ID to search against.

A simple solution would be something like this, but it doesn't work like that.

var currentsection = /'{"title":"' + edittitle + '","link":"' + editurl + '","cssclass":"' + editcssclass + '","cssid":"' + editcssid + '","id":"' + editid + '"}'/;
var newsection = /'{"title":"' + updatedtitle + '","link":"' + updatedlink + '","cssclass":"' + updatedcssclass + '","cssid":"' + updatedcssid + '","id":"' + updatedid + '"}'/;

$("#menu_items").val().find(currentsection).replaceWith(newsection);

What do you think the best approach is? Many thanks for taking the time out to help. I really appreciate it.

share|improve this question
    
Save JSON into DB sounds weird ... and why don't work directly with JSON objects ? –  Maxime May 5 '14 at 11:11
    
Thanks Maxime. I'm saving JSON to the db for easy use later on with an API. Regarding working with the JSON objects directly, what would you suggest the best approach to finding and replacing part of it would be? I'm no Jquery wizard, so a little unsure of the best thing to look for. Cheers. –  T2theC May 5 '14 at 11:19

1 Answer 1

I think you should create your JSON object, and work with it. In this way it would be easy to change values and also save it as you want ;)

For example :

var json = YOUR JSON HERE;
var obj = JSON.parse(json);

// now you can update values as you want
// for example with example title
obj[0].title = "updatetitle";

And then, before sending your JSON, you may want to convert it in plain text

var json = JSON.stringify(obj);
share|improve this answer
    
Does it helped ? :) –  Maxime May 5 '14 at 12:09
    
Thanks Maxime. Yes it is helping - to a degree. It is fine is I specify a obj[x] value on obj. However, if the element is a child of obj[1] for example, it naturally can't find it to update. So I will need to right some additional logic to get it to work. Many thanks for your contribution. –  T2theC May 5 '14 at 14:46
    
Any example ? Some code snippet ? Because with the JSON you've written in your post that should work. If you have an array inside an array, just do my_array[index1][index2] etc ... ;) –  Maxime May 6 '14 at 6:12

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.