Json 返回 [object object] 而不是值

我正在尝试提取 json 的值,但是当我返回它时,我得到了一个对象对象。我在解码时做错了什么?这是php中的解码代码


<?php $contenido=file_get_contents("https://www.deperu.com/api/rest/cotizaciondolar.json");

$info = json_decode($contenido,true);   

$cadena=array(

        0=>$info['cotizacion'],

    );

    echo json_encode($cadena);      

?> 

这是功能代码



<script>

    $(function() {

        $("#btnbuscar").on('click',function(){          


           var direccion='servicio.php';

            $.ajax({

                type:'get',

                url:direccion,


                success:function(datos){


                    var campo=eval(datos);


                    alert(datos[0]);


            }

            });

            return false;

        });

    });

</script>


侃侃无极
浏览 152回答 4
4回答

当年话下

有几种解决方案,但在调用 json 链时不要在javascript/jquery中出错。例如:<?php// Page : service.php$json = '{&nbsp; &nbsp; "service": "Reference dollar exchange rate",&nbsp; &nbsp; "website": "website.com",&nbsp; &nbsp; "link": "https://www.website.com/gearbox_type/",&nbsp; &nbsp; "quotation": [{&nbsp; &nbsp; &nbsp; &nbsp; "buy": 3.419,&nbsp; &nbsp; &nbsp; &nbsp; "sale": 3.424&nbsp; &nbsp; }]}';// $json = file_get_contents("https://www.website.com/api/example.json");$info = json_decode($json,true); // convert array$cadena=array(&nbsp; &nbsp; 0=>$info['quotation'][0],);echo json_encode($cadena); // convert json// get-> [{"buy":3.419,"sale":3.424}]echo json_encode($cadena[0]); // convert json// get-> {"buy":3.419,"sale":3.424}?>// Javascript&nbsp;// To better use your function I would have to do a cleanup of the code with JSON.parse<script>&nbsp; &nbsp; $(function() {&nbsp; &nbsp; &nbsp; &nbsp; /*&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;* Check yes and Json and convert json string&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;* Analyze the data with JSON.parse () and the data becomes a JavaScript object.&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;* Ex. var obj = '{hello:'mitico'}' -> convert object&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;* $.clean_string_json(obj) return-> {hello:'mitico'}&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;* $.clean_string_json('text' + obj) return-> {}&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;* $.clean_string_json('text' + obj,false) return-> false&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;* $.clean_string_json('text' + obj,true) return-> true&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;*/&nbsp; &nbsp; &nbsp; &nbsp; $.clean_string_json = function (str,xreturn) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; try {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return JSON.parse(str);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } catch (e) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return xreturn === false ? false : xreturn || {};&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; };&nbsp; &nbsp; &nbsp; &nbsp; $("#btnbuscar").on('click',function(){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $.ajax({&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; type:'get',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; url: 'service.php',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; success:function(datos){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var campo= $.clean_string_json(datos);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; alert(datos[0]); // return -> {"buy":3.419,"sale":3.424}&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return false;&nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; });</script>

largeQ

欢迎来到计算器!我们希望您喜欢这里。正如@Anurag Srivastava 已经指出的那样,直接调用 url,你会得到 json,你不需要代理。const jUrl = "https://www.deperu.com/api/rest/cotizaciondolar.json";$.get(jUrl).then(({cotizacion}) => console.log(cotizacion));<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

ABOUTYOU

当你这样写的时候:$cadena=array(&nbsp; &nbsp; 0=>$info['cotizacion'],);echo json_encode($cadena);你的 $info 是一个数组,cadena 也是一个数组。所以你可以像这样将 $cadenra 指向数组:$cadena= $info['cotizacion'];echo json_encode($cadena);或者像这样修复你的js:alert(datos[0][0]);

哆啦的时光机

这是一种无需 Ajax 但使用的简单读取 JSON 的方法$.getJSON在您的 PHP 文件中,因为您只想获得“cotizacion”数据更改:$cadena=array(0=>$info['cotizacion']如果$cadena=array(0=>$info['cotizacion'][0]您计划拥有并循环多个“cotizacion”,则可以删除 [0]在你的 javascript 上使用:$.getJSON("servicio.php", function(data) {&nbsp; var items = [];&nbsp; $.each(data[0], function(key, val) {&nbsp; &nbsp; (key + '=' + val);&nbsp; });});
打开App,查看更多内容
随时随地看视频慕课网APP