转换JSON字符串数组但不json_de code [英] Convert JSON string to array WITHOUT json_decode

查看:206
本文介绍了转换JSON字符串数组但不json_de code的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的共享服务器上通过PHP是返回一个包含2级数据的JSON API来访问外部站点(1级:表演和放大器; 2级:里面表演类数组)。我想将其转换为多维关联数组,而无需使用json_de code函数(它使用了太多的内存为这种用法!)

JSON数据示例:

  [
{
    performerId:99999,
    名:任何表演者的名字,
    类别:{
        的categoryId:99,
        名:有些类别名称,
        EVENTTYPE:事件类
    },
    EVENTTYPE:演员事件,
    URL:http://www.novalidsite.com/something/performerspage.html
    优先级:0
},
{
    performerId:88888,
    名:第二次表演的名字
    类别:{
        的categoryId:88,
        名:第二类名,
        EVENTTYPE:类别事件2
    },
    EVENTTYPE:演员事件2
    URL:http://www.novalidsite.com/somethingelse/performerspage2.html
    优先级:7
}
]

我曾尝试使用SUBSTR和剥离[和]。

然后进行呼叫:

  preg_match_all('/ \\ {([^}] +)\\} /',$投入,$匹配);

这让我对每一行的字符串,但尾随后截断}一类的数据。

我怎样才能返回数据的全行作为使用像preg_split,preg_match_all等,而不是重手呼叫像整体JSON字符串json_de code数组?

一旦我有正确识别每一行的数组,然后我就可以执行该字符串json_de code,而不共享服务器上暴敛内存。


对于那些想要更详细了解json_de code的使用导致错误:

  $ aryPerformersfile [] =的file_get_contents('https://subdomain.domain.com/dir/getresults?id=1234');
$ aryPerformers = $ aryPerformersfile [0];
未设置($ aryPerformersfile);
$ mytmpvar = json_de code($ aryPerformers);
的print_r($ mytmpvar);
出口;


解决方案

如果你的内存是有限的,你可以读取数据流,并解析JSON一块的时间,而不是在一次解析一切

getresults.json:

  [
    {
        performerId:99999,
        名:任何表演者的名字,
        类别:{
            的categoryId:99,
            名:有些类别名称,
            EVENTTYPE:事件类
        },
        EVENTTYPE:演员事件,
        URL:http://www.novalidsite.com/something/performerspage.html
        优先级:0
    },
    {
        performerId:88888,
        名:第二次表演的名字
        类别:{
            的categoryId:88,
            名:第二类名,
            EVENTTYPE:类别事件2
        },
        EVENTTYPE:演员事件2
        URL:http://www.novalidsite.com/somethingelse/performerspage2.html
        优先级:7
    }
]

PHP:

  $ =流的fopen('getresults.json','RB');//从$流,直到一次读一个字符
// $的字符的字符$计数读
功能readUpTo($流,$字符,$计数)
{
    $海峡='';
    $ foundCount = 0;
    而(!的feof($流)){
        $ readChar = stream_get_contents($流,1);        $海峡= $ readChar。
        如果($ readChar == $字符和放大器;&安培; ++ $ foundCount == $计数)
            返回$海峡;
    }
    返回false;
}//读取一个JSON对象演员
功能readOneJsonPerformer($流)
{
    如果($ JSON = readUpTo($流,{,1))
        返回{。 readUpTo($流,},2);
    返回false;
}而($ JSON = readOneJsonPerformer($流)){
    $表演= json_de code($ JSON);    回声的表演者ID。 $ performer-> performerId
        。 有类别。 $ performer->&类别 - GT;名,PHP_EOL;
}
FCLOSE($流);

输出:

 演员ID为99999有一些类别类别名称
表演者ID 88888有类别二类别名称

这code当然可以通过使用缓冲区更快的读取速度,考虑到字符串值本身可能包括提高 {} 字符等。

I am using PHP on shared server to access external site via API that is returning JSON containing 2 levels of data (Level 1: Performer & Level 2: Category array inside performer). I want to convert this to multidimensional associative array WITHOUT USING json_decode function (it uses too much memory for this usage!!!)

Example of JSON data:

[
{
    "performerId": 99999,
    "name": " Any performer name",
    "category": {
        "categoryId": 99,
        "name": "Some category name",
        "eventType": "Category Event"
    },
    "eventType": "Performer Event",
    "url": "http://www.novalidsite.com/something/performerspage.html",
    "priority": 0
},
{
    "performerId": 88888,
    "name": " Second performer name",
    "category": {
        "categoryId": 88,
        "name": "Second Category name",
        "eventType": "Category Event 2"
    },
    "eventType": "Performer Event 2",
    "url": "http://www.novalidsite.com/somethingelse/performerspage2.html",
    "priority": 7
}
]

I have tried to use substr and strip the "[" and "]".

Then performed the call:

preg_match_all('/\{([^}]+)\}/', $input, $matches);

This gives me the string for each row BUT truncates after the trailing "}" of the category data.

How can I return the FULL ROW of data AS AN ARRAY using something like preg_split, preg_match_all, etc. INSTEAD of the heavy handed calls like json_decode on the overall JSON string?

Once I have the array with each row identified correctly, I CAN THEN perform json_decode on that string without overtaxing the memory on the shared server.


For those wanting more detail about json_decode usage causing error:

$aryPerformersfile[ ] = file_get_contents('https://subdomain.domain.com/dir/getresults?id=1234');
$aryPerformers = $aryPerformersfile[0];
unset($aryPerformersfile);
$mytmpvar = json_decode($aryPerformers);
print_r($mytmpvar);
exit;

解决方案

If you have a limited amount of memory, you could read the data as a stream and parse the JSON one piece at a time, instead of parsing everything at once.

getresults.json:

[
    {
        "performerId": 99999,
        "name": " Any performer name",
        "category": {
            "categoryId": 99,
            "name": "Some category name",
            "eventType": "Category Event"
        },
        "eventType": "Performer Event",
        "url": "http://www.novalidsite.com/something/performerspage.html",
        "priority": 0
    },
    {
        "performerId": 88888,
        "name": " Second performer name",
        "category": {
            "categoryId": 88,
            "name": "Second Category name",
            "eventType": "Category Event 2"
        },
        "eventType": "Performer Event 2",
        "url": "http://www.novalidsite.com/somethingelse/performerspage2.html",
        "priority": 7
    }
]

PHP:

$stream = fopen('getresults.json', 'rb');

// Read one character at a time from $stream until
// $count number of $char characters is read
function readUpTo($stream, $char, $count)
{
    $str = '';
    $foundCount = 0;
    while (!feof($stream)) {
        $readChar = stream_get_contents($stream, 1);

        $str .= $readChar;
        if ($readChar == $char && ++$foundCount == $count)
            return $str;
    }
    return false;
}

// Read one JSON performer object
function readOneJsonPerformer($stream)
{
    if ($json = readUpTo($stream, '{', 1))
        return '{' . readUpTo($stream, '}', 2);
    return false;
}

while ($json = readOneJsonPerformer($stream)) {
    $performer = json_decode($json);

    echo 'Performer with ID ' . $performer->performerId
        . ' has category ' . $performer->category->name, PHP_EOL;
}
fclose($stream);

Output:

Performer with ID 99999 has category Some category name
Performer with ID 88888 has category Second Category name

This code could of course be improved by using a buffer for faster reads, take into account that string values may themselves include { and } chars etc.

这篇关于转换JSON字符串数组但不json_de code的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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