我试图从div中获取一些JSON,其中一部分可以使用,但另一部分不会。
function downloadURL(){
var curseDom = 'http://www.curse.com/mc-mods/minecraft/';
var submittedURL = $('#download').val();
var domCheck = submittedURL.substring(0,39);
var getJSON = submittedURL.replace("http://www.curse.com/mc-mods/minecraft/", "http://widget.mcf.li/mc-mods/minecraft/") + ".json";
if(domCheck === curseDom){
urls = [];
urls.push(getJSON);
$(urls).each(function(i, item){
$.get(item, function(data){
if (typeof data === "object") {
data = JSON.stringify(data);
}
$("#output").append(data).append('<hr>');
});
});
//Show the mod version selection div
$(".hide1").show();
}else
//Will move to a more suitable place for user
$("#output").append("Invalid URL");
}
//Refresh Selector to be able to show jar versions with URLs as values
function refreshSelector(){
var text = document.getElementById("output").textContent;
//JSON parse
var obj = jQuery.parseJSON(text);
//Add version selector
var $select = $('#mySelectID');
$(obj).each(function (index, o) {
var $option = $("<option/>").attr("value", o.url).text(o.name);
$select.append($option);
});
}
//Time to make a new JSON
//Get the output of the modpack JSON
function addToJSON(){
var text = document.getElementById("output").textContent;
//JSON parse
var obj = jQuery.parseJSON(text);
//Get the pack title?
var modtitle = obj.title;
//Make modpack title (and version, at a later date, visible to the user)
$("#see").append(modtitle + "<br>");
//Remove the previous mod JSON
$( "#output" ).empty();
//Hide this div again
$(".hide1").hide();
}
&#13;
<!-- Pack Tool -->
<p> <input type="text" name="download" id="download" placeholder="Download URL"></p>
<button onclick="downloadURL()">Select Mod Version</button>
<p> </p>
<div id="" class="hide1">
<!-- Div hidden by default, will show when a URL has been added --> <select id="mySelectID" name="select" class="main-form">
<option value="">None Selected</option>
</select>
<button onclick="addToJSON()">Add to Modpack</button>
</div>
<div id="output"><!-- Example of what a JSON in this div will look like -->
{"title":"Blood Magic ","game":"Minecraft","category":"Magic","url":"http://www.curse.com/mc-mods/minecraft/224791-blood-magic","thumbnail":"https://media-curse.cursecdn.com/attachments/129/29/2f94367c7e79ef30ad6fab3cd9836332.png","authors":["WayofTime"],"downloads":{"monthly":163723,"total":1529462},"favorites":195,"likes":10,"updated_at":"2015-10-31T21:21:41+0000","created_at":"2014-10-09T19:07:30+0000","project_url":"http://www.curseforge.com/projects/224791/","release_type":"Release","license":"Custom License","download":{"id":2264826,"url":"http://curse.com/mc-mods/minecraft/224791-blood-magic/2264826","name":"BloodMagic-1.7.10-1.3.3-17.jar","type":"release","version":"1.7.10","downloads":73933,"created_at":"2015-10-31T21:14:42+0000"},"versions":{"1.7.10":[{"id":2264826,"url":"http://curse.com/mc-mods/minecraft/224791-blood-magic/2264826","name":"BloodMagic-1.7.10-1.3.3-17.jar","type":"release","version":"1.7.10","downloads":73933,"created_at":"2015-10-31T21:14:42+0000"},{"id":2247348,"url":"http://curse.com/mc-mods/minecraft/224791-blood-magic/2247348","name":"BloodMagic-1.7.10-1.3.3-5.jar","type":"release","version":"1.7.10","downloads":286,"created_at":"2015-07-16T13:51:39+0000"},{"id":2247347,"url":"http://curse.com/mc-mods/minecraft/224791-blood-magic/2247347","name":"BloodMagic-1.7.10-1.3.3-5.jar","type":"release","version":"1.7.10","downloads":15962,"created_at":"2015-07-16T13:51:39+0000"},{"id":2241377,"url":"http://curse.com/mc-mods/minecraft/224791-blood-magic/2241377","name":"BloodMagic-1.7.10-1.3.3-4.jar","type":"release","version":"1.7.10","downloads":69851,"created_at":"2015-06-04T21:30:41+0000"}}}</div>
<div id="see"><strong>Mods added to pack:</strong><br></div>
&#13;
我在输出div中添加了一个JSON,这样你就可以看到我想要从中获取什么。
我所拥有的代码将在&#34;中看到&#34;中显示修改标题。 DIV。但是,选择器代码不起作用。因为它应该添加如下内容:
<option value="http://curse.com/mc-mods/minecraft/224791-blood-magic/2264826">BloodMagic-1.7.10-1.3.3-17.jar</option>
但它并没有添加任何东西。但是,我知道JSON正被解析为&#34; obj.title&#34; (在示例中&#34; Blood Magic&#34;被添加到div&#34;请参阅&#34;)
答案 0 :(得分:1)
需要添加:
obj.versions["XXXX"];
然后调用版本而不是obj
var obj2 = obj.versions["XXXX"];
$(obj2).each(function (index, o) {