douqie1816 2019-07-19 17:10
浏览 87

从文件中每行解码多个json

We are trying to extract certain information of multiple json objects written into a file. There is one per line, and need to get some strings from each of them and print specific information.

This JSON format is very strange, I would like to know if there is a code to achieve results we need

{'creationDate': None, 'endTime': 1563754500.0, 'expirationDate': 1563743700.0, 'isPriorityOffer': False, 'offerId': 'AAGutxx7466', 'offerType': 'NON_EXCLUSIVE', 'rateInfo': {'currency': 'USD', 'isSurge': False, 'priceAmount': 54.0, 'projectedTips': 0.0, 'surgeMultiplier': None}, 'schedulingType': 'BLOCK', 'serviceAreaId': 'dd00ctyy', 'serviceTypeId': 'PuyOplzlR1idvfPkv5138g', 'serviceTypeMetadata': {'nameClassification': 'STANDARD'}, 'startTime': 1563743700.0, 'startingLocation': {'address': {'address1': '', 'address2': None, 'address3': None, 'addressId': None, 'city': None, 'countryCode': None, 'name': None, 'phone': None, 'postalCode': None, 'state': None}, 'geocode': {'latitude': 0.0, 'longitude': 0.0}, 'locationType': None, 'startingLocationName': ''}, 'status': 'OFFERED', 'trIds': None}
{'creationDate': None, 'endTime': 1563754500.0, 'expirationDate': 1563741900.0, 'isPriorityOffer': False, 'offerId': 'AAGutxx8547', 'offerType': 'NON_EXCLUSIVE', 'rateInfo': {'currency': 'USD', 'isSurge': False, 'priceAmount': 63.0, 'projectedTips': 0.0, 'surgeMultiplier': None}, 'schedulingType': 'BLOCK', 'serviceAreaId': '50ade699', 'serviceTypeId': 'PuyOplzlR1idvfPkv5138g', 'serviceTypeMetadata': {'nameClassification': 'STANDARD'}, 'startTime': 1563741900.0, 'startingLocation': {'address': {'address1': '', 'address2': None, 'address3': None, 'addressId': None, 'city': None, 'countryCode': None, 'name': None, 'phone': None, 'postalCode': None, 'state': None}, 'geocode': {'latitude': 0.0, 'longitude': 0.0}, 'locationType': None, 'startingLocationName': ''}, 'status': 'OFFERED', 'trIds': None}

I tried this code, but is not working wih that json structure

<?

function readJson($File){

  // open the file to with the R flag,
    $Path = fopen($File,"r");

    // if file found,
    if ($Path) {
        $print = '';

            // for each line
            while (($line = fgets($Path)) !== false) {
                $Output = json_decode($line);
                $print .= "Service Area: ".$Output->serviceAreaId."<br/>";
                $print .= "Start time: ".$Output->startTime."<br/>";
                $print .= "Price: ".$Output->priceAmount."<hr>";
            }

        fclose($Path);
    } 

    return $print;
}

echo readJson("logs.txt");

?>
  • 写回答

3条回答 默认 最新

  • doushe2513 2019-07-19 17:30
    关注

    Please change your JSON-Strings to a valid JSON:

    {"creationDate": null, "endTime": 1563754500.0, "expirationDate": 1563743700.0, "isPriorityOffer": false, "offerId": "AAGutxx7466", "offerType": "NON_EXCLUSIVE", "rateInfo": {"currency": "USD", "isSurge": false, "priceAmount": 54.0, "projectedTips": 0.0, "surgeMultiplier": null}, "schedulingType": "BLOCK", "serviceAreaId": "dd00ctyy", "serviceTypeId": "PuyOplzlR1idvfPkv5138g", "serviceTypeMetadata": {"nameClassification": "STANDARD"}, "startTime": 1563743700.0, "startingLocation": {"address": {"address1": "", "address2": null, "address3": null, "addressId": null, "city": null, "countryCode": null, "name": null, "phone": null, "postalCode": null, "state": null}, "geocode": {"latitude": 0.0, "longitude": 0.0}, "locationType": null, "startingLocationName": ""}, "status": "OFFERED", "trIds": null}
    

    Also you have to access the price via

    $print .= "Price: ".$Output->rateInfo->priceAmount."<hr>";
    

    On my machine the code is running after changing

    评论

报告相同问题?

悬赏问题

  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示