0

I have a text that includes mani list items on the next format:

var text = "<li>M3-2200 (da2/M3-2200)</li><li>N3-2200 (da2/N3-2200)</li><li>Picasso (picasso/A500)</li><li>Picasso (picasso/A501)</li><li>Picasso (ventana/A500)</li>..."

I'm trying to create JSON on the next format:

{
 name: "M3-2200",
 Model: "M3-2200" 
}

I'm trying using next code, but it doesn't work my problem is on push. anybody can explain me how do it right?

result ={};
while(text.indexOf("<li>")!== -1){
 var listi = text.substring(text.indexOf("<li>"), text.indexOf("</li>"));
 var model = listi.substring(0, listi.indexOf("(") -1);
 var name = listi.substring(listi.indexOf("("), listi.indexOf(")"));
 var item = {name: name: model : model};
 result.push(item);
 var text = text.substring(text.indexOf("</li>"));
}
mshsayem
18.1k11 gold badges65 silver badges73 bronze badges
asked Apr 28, 2014 at 8:22
1
  • There was an answer suggesting result=[]; (instead of result={};) -- that is correct, result should be an array. Commented Apr 28, 2014 at 8:30

3 Answers 3

1

Other solution for your problem:

var text = "<li>M3-2200 (da2/M3-2200)</li><li>N3-2200 (da2/N3-2200)</li><li>Picasso (picasso/A500)</li><li>Picasso (picasso/A501)</li><li>Picasso (ventana/A500)</li>";
var result = JSON.parse('[' + 
 text.replace(/(<li>|<\/li>| \(|\))/g, function(_, part){
 switch (part) {
 case '<li>': return '{"name":"';
 case '</li>': return '},';
 case ' (': return '", "Model":"';
 case ')': return '"';
 }
 }) + '0]').slice(0, -1);
answered Apr 28, 2014 at 8:45
Sign up to request clarification or add additional context in comments.

Comments

1
var text = "<li>M3-2200 (da2/M3-2200)</li><li>N3-2200 (da2/N3-2200)</li><li>Picasso (picasso/A500)</li><li>Picasso (picasso/A501)</li><li>Picasso (ventana/A500)</li>";
JSONStr = text.trim().replace(/<li>/g,"{\"name\":\"").replace(/ \(/g,"\" , \"model\":\"").replace(/\)\<\/li\>/g,"\"},");
JSONStr = "["+JSONStr.substring(0,JSONStr.length-1)+"]";
console.log(JSONStr);

Will the above code work?

answered Apr 28, 2014 at 9:33

2 Comments

I think this is the best solution. But why JSONStr.substring(0,JSONStr.length-1) instead of just JSONStr on line 3?
The code adds a "," at the end. I wanted to take that out.
0

I hope I understand your question correctly. If this is the output you want:

[{"name":"M3-2200 ","model":"da2/M3-2200"},{"name":"N3-2200 ","model":"da2/N3-2200"},{"name":"Picasso ","model":"picasso/A500"},{"name":"Picasso ","model":"picasso/A501"},{"name":"Picasso ","model":"ventana/A500"}]

Then this is a way to do it:

var text = "<li>M3-2200 (da2/M3-2200)</li><li>N3-2200 (da2/N3-2200)</li><li>Picasso (picasso/A500)</li><li>Picasso (picasso/A501)</li><li>Picasso (ventana/A500)</li>";
var extractItem = function (item) {
 var partsArray = /(.+) ?\((.+)\)\<\/li>/.exec(item)
 if(partsArray) return {"name":partsArray[1], "model":partsArray[2]}
}
var result = text.split('<li>')
 .map(extractItem)
 .filter(function(e){return e != undefined});
console.log(JSON.stringify(result));
answered Apr 28, 2014 at 8:49

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.