简体   繁体   English

如何解析json以从URL获得lodash分数?

[英]how to parse json to get lodash score from a url?

I'm trying to find out how to parse the url and put the names in an array and then to parse the json to get the lodash score and convert the score to a whole number. 我试图找出如何解析url并将名称放入数组中,然后解析json以获取lodash得分并将得分转换为整数。 any help would be nice. 你能帮忙的话,我会很高兴。

<head>
    <title> final</title>
    <script src="https://code.jquery.com/jquery-3.2.1.min.js"></script>
    <!-- Latest compiled and minified CSS -->
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
    <!-- Optional theme -->
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap-theme.min.css" integrity="sha384-rHyoN1iRsVXV4nD0JutlnGaslCJuC7uwjduW9SVrLvRYooPp2bWYgmgJQIXwl/Sp" crossorigin="anonymous">
</head>

<body>
    <h3>The window.location object</h3>

<input type="button" value="Load new document" onclick="newDoc()">

<script>
function newDoc() {
    window.location.assign("https://api.npms.io/v2/package/lodash");
}

</script>
    <!--// JSON.parse(");-->
    <!--// displayfromJSON(lodash);-->
    <!--//     window.substring(0.9200443085079093);-->
    <!--// split(92)-->
</body>

</html>

When you change the location to that url your page no longer exists...and neither does any of your script 当您将位置更改为该URL时,您的页面将不再存在...而且您的任何脚本也都不存在

Use fetch() to get the data and then do what you want with it. 使用fetch()获取数据,然后对数据进行所需的处理。

 fetch('https://api.npms.io/v2/package/lodash') .then(res => res.json()) .then(data => console.log(data.score)) 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM