我有一串混合内容,如:
{json stuff}来自同一输出的其他内容有< html>标签喜欢 < div> some< / div>还有更多东西,最后还有另外一个{json stuff}
所以我需要的是能够使用这3个元素:
所以为了得到第一个json,我这样做:
$first_json = json_decode(substr($mixed_string,0, strpos($mixed_string,"}")+1 ));
// that will get me the first json content
// Then I need the second json which is at the end of the string,
// the length of the string is unknown , the json length is unknown
// after that I just do:
echo preg_replace("/\{[^)]+\}/","",$mixed_string);
// and I get the html from that string...
那么,我怎样才能得到最后一个json?
答案 0 :(得分:0)
你可以使用正则表达式来获取JSON值,它可以让你从任何字符串中获取JSON,无论长度如何。假设您的{
"publishOptions": {
"include": [
"wwwroot",
"Views",
"Areas/**/Views",
"settings.json",
"settings.production.json",
"web.config"
]
}
}
内容ID为div
,div
包含您要提取的JSON:
编辑:将提取过程添加为函数。
function extractJSON(content) {
var regex = /({.*?})/g,
raw = content.match(regex),
output = [],
errors = [];
raw.forEach(function(el) {
try {
output.push(JSON.parse(el));
} catch (e) {
errors.push('issue with object: ' + el);
}
});
return {
results: output,
errors: errors
}
}
// Grab the elements that contain JSON
var elements = document.getElementById('content').innerHTML;
console.log(extractJSON(elements));