PHP:Json解码问题 [英] PHP : Json decoding question

查看:99
本文介绍了PHP:Json解码问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在没有php内存限制的情况下解码整个feed约10k项? 我找到了将提要分解为一部分的代码: http://pastebin.com/m43c3384f

How do i decode whole feed arround 10k item without memory limit of php? i found a code which break the feed to part: http://pastebin.com/m43c3384f

如何修改代码以与以下供稿一起使用:

how do modify the code to work with the feed below :

GAF_update_projects_vertical_callback({"projects":{"count":2,"items":[{"id":502817,"name":"SEO","url":"http:\/\/www.getafreelancer.com\/affiliates\/ref.php?u=boon86&l=http%3A%2F%2Fwww.getafreelancer.com%2Fprojects%2FSEO%2FSEO.502817.html","start_unixtime":1252386582,"start_date":"Tue, 08 Sep 2009 01:09:42 -0400","end_unixtime":1252818582,"end_date":"Sun, 13 Sep 2009 01:09:42 -0400","buyer":{"url":"http:\/\/www.getafreelancer.com\/affiliates\/ref.php?u=boon86&l=http%3A%2F%2Fwww.getafreelancer.com%2Fusers%2F1094422.html","id":1094422,"username":"eyalbiber"},"state":"A","short_descr":"Hi,  My name is Eyal.  I'm looking for seo expert to work with him on long term relationship.  I need...","options":{"featured":0,"nonpublic":0,"trial":0,"fulltime":0,"for_gold_members":0,"hidden_bids":0},"budget":{"min":30,"max":250},"jobs":["SEO"],"bid_stats":{"count":0,"avg":false}},{"id":503874,"name":"websubmissionarticles","url":"http:\/\/www.getafreelancer.com\/affiliates\/ref.php?u=boon86&l=http%3A%2F%2Fwww.getafreelancer.com%2Fprojects%2FCopywriting%2Fwebsubmissionarticles.html","start_unixtime":1252386528,"start_date":"Tue, 08 Sep 2009 01:08:48 -0400","end_unixtime":1252559328,"end_date":"Thu, 10 Sep 2009 01:08:48 -0400","buyer":{"url":"http:\/\/www.getafreelancer.com\/affiliates\/ref.php?u=boon86&l=http%3A%2F%2Fwww.getafreelancer.com%2Fusers%2F39628.html","id":39628,"username":"seexpert"},"state":"A","short_descr":"need some1 to rewrite articles by taking them from the net from various min 5 sources. he\/she can draft...","options":{"featured":0,"nonpublic":0,"trial":0,"fulltime":0,"for_gold_members":0,"hidden_bids":0},"budget":{"min":30,"max":250},"jobs":["Copywriting"],"bid_stats":{"count":0,"avg":false}}]}})

我在下面尝试了此代码:

I tried this code below :

<?php
require("JSON.php");

$mochi_url = "http://api.getafreelancer.com/Project/Search.json?aff=480992&callback=GAF_update_projects_vertical_callback&order=submitdate&count=50&iads=true&nonpublic=0&charset=UTF-8";
$feed = file_get_contents($mochi_url);

$feed_start=strpos($feed,"[{");

$feed = substr($feed,$feed_start+1);
$feed = substr($feed,0,strlen($feed)-2);
$games = explode("{\"id\": \"",$feed);
unset($feed);
for($x=1;$x<=count($games);$x++){
     $games[$x] = "{\"name\": \"".$games[$x];
     $games[$x] = substr($games[$x],0,strlen($games[$x])-2);
     $games_decode = json_decode($games[$x], true);

     foreach($games_decode as $varname => $varvalue) {
          $mochi[$varname] = $varvalue;
     }

                $name = $mochi['name'];



                  echo "$x - Name: {$mochi['name']} <br>"; 
                unset ($games[$x]);
}

?> 

它还给我

警告:第18行的/home/studiox/public_html/getafreelancer/auto.php中为foreach()提供的参数无效 1-名称:

Warning: Invalid argument supplied for foreach() in /home/studiox/public_html/getafreelancer/auto.php on line 18 1 - Name:

有什么主意吗?

推荐答案

首先,转义json字符串.像这样:

First of all, escape your json string. Like this:

$json_string = "{\"projects\":{\"count\":2,\"items\":[{\"id\":502817,\"name\":\"SEO\",\"url\":\"http:\/\/www.getafreelancer.com\/affiliates\/ref.php?u=boon86&l=http%3A%2F%2Fwww.getafreelancer.com%2Fprojects%2FSEO%2FSEO.502817.html\",\"start_unixtime\":1252386582,\"start_date\":\"Tue, 08 Sep 2009 01:09:42 -0400\",\"end_unixtime\":1252818582,\"end_date\":\"Sun, 13 Sep 2009 01:09:42 -0400\",\"buyer\":{\"url\":\"http:\/\/www.getafreelancer.com\/affiliates\/ref.php?u=boon86&l=http%3A%2F%2Fwww.getafreelancer.com%2Fusers%2F1094422.html\",\"id\":1094422,\"username\":\"eyalbiber\"},\"state\":\"A\",\"short_descr\":\"Hi,  My name is Eyal.  I'm looking for seo expert to work with him on long term relationship.  I need...\",\"options\":{\"featured\":0,\"nonpublic\":0,\"trial\":0,\"fulltime\":0,\"for_gold_members\":0,\"hidden_bids\":0},\"budget\":{\"min\":30,\"max\":250},\"jobs\":[\"SEO\"],\"bid_stats\":{\"count\":0,\"avg\":false}},{\"id\":503874,\"name\":\"websubmissionarticles\",\"url\":\"http:\/\/www.getafreelancer.com\/affiliates\/ref.php?u=boon86&l=http%3A%2F%2Fwww.getafreelancer.com%2Fprojects%2FCopywriting%2Fwebsubmissionarticles.html\",\"start_unixtime\":1252386528,\"start_date\":\"Tue, 08 Sep 2009 01:08:48 -0400\",\"end_unixtime\":1252559328,\"end_date\":\"Thu, 10 Sep 2009 01:08:48 -0400\",\"buyer\":{\"url\":\"http:\/\/www.getafreelancer.com\/affiliates\/ref.php?u=boon86&l=http%3A%2F%2Fwww.getafreelancer.com%2Fusers%2F39628.html\",\"id\":39628,\"username\":\"seexpert\"},\"state\":\"A\",\"short_descr\":\"need some1 to rewrite articles by taking them from the net from various min 5 sources. he\/she can draft...\",\"options\":{\"featured\":0,\"nonpublic\":0,\"trial\":0,\"fulltime\":0,\"for_gold_members\":0,\"hidden_bids\":0},\"budget\":{\"min\":30,\"max\":250},\"jobs\":[\"Copywriting\"],\"bid_stats\":{\"count\":0,\"avg\":false}}]}}";

PHP具有本机JSON支持.您可以使用json_decode()在PHP关联数组中直接解码JSON字符串:

PHP has native JSON support. You can decode JSON string directly in PHP associative array using json_decode():

$result = json_decode($json_string, true);

测试:

echo $result['projects']['items'][0]['name'];
echo '<br>';
echo $result['projects']['items'][0]['url'];

结果:

SEO
http://www.getafreelancer.com/affiliates/ref.php?u=boon86&l=http%3A%2F%2Fwww.getafreelancer.com%2Fprojects%2FSEO%2FSEO.502817.html


更新:

正在回答的问题:如果我在JSON feed中有很多项目并且想要循环每个项目以存储在mysql中,该怎么办.

Answering question: What to do if i have many projects inside the JSON feed and i want to loop each one to store in mysql.

这实际上与JSON不相关,而与PHP数组有关.您可以使用许多嵌入PHP的便捷函数.在这种情况下,您所需要做的就是知道项目的数组元素计数(读取:项目数量),然后使用简单循环从数组中提取所有数据.

This is really not related to JSON, but rather to PHP arrays. There is a lot of handy function embedded into PHP that you may use. In this case all you need is to know array element count for your projects (read: amount of projects), then extract all the data out of your array using simple loop.

for($i = 0; $i < count($result['projects']['items']); $i++) {
  $project_name = $result['projects']['items'][$i]['name'];
  $project_id   = $result['projects']['items'][$i]['id'];
  $project_date = $result['projects']['items'][$i]['start_date'];

  // save / print / do whatever you want with this data
  echo "Project : $project_name [$project_id] - $project_date<br>";
}

P.S.好的PHP数组分步教程是在此处.

P.S. Nice PHP array step-by-step tutorial is here.

这篇关于PHP:Json解码问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆