Solidity oraclize解析Json格式数据


solidity虽然不能解析json数据但是oraclize_query可以直接处理:

pragma solidity ^0.4.21;
import "github.com/oraclize/ethereum-api/oraclizeAPI.sol";

contract oraclizeJson is usingOraclize{
    event logString(string);
    event logUint(uint);
    string public str = strConcat("http://119.28.70.201:8792/getprice/?index=","DJI","&date=",uint2str(17623));
    
    function strConcat(){
        bytes32 queryId = oraclize_query("URL", "json(http://api.k780.com/?app=finance.globalindex&inxno=000001&appkey=10003&sign=b59bc3ef6191eb9f747dd4e83c99f2a4&format=json).result.yesy_price");  //oraclize解析json数据      
    }
    
    function __callback(bytes32 myid, string result, bytes proof) public{
        emit logString(result);
        uint i = parseInt(result,3); //string to uint转换,
        emit logUint(i+1);
    }
}

 

 

查询获得的Json数据(http://api.k780.com/?app=finance.globalindex&inxno=000001&appkey=10003&sign=b59bc3ef6191eb9f747dd4e83c99f2a4&format=json),在oraclize query中用 “.result.yesy_price” 可以取到yesy_price数据

{
    "success": "1", 
    "result": {
        "inxid": "1", 
        "typeid": "hs", 
        "inxno": "000001", 
        "inxnm": "上证指数", 
        "yesy_price": "3136.63", 
        "open_price": "3147.05", 
        "last_price": "3131.11", 
        "change_price": "-5.52", 
        "change_price_per": "-0.18%", 
        "high_price": "3163.34", 
        "low_price": "3128.87", 
        "amplitude_price_per": "1.10%", 
        "uptime": "2018-04-04 15:34:58"
    }
}

 

posted @ 2018-04-08 15:39  huahuayu  阅读(3032)  评论(0编辑  收藏  举报