Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.php > #18764

Re: JSON encode "corrupted"?

From Arno Welzel <usenet@arnowelzel.de>
Newsgroups comp.lang.php
Subject Re: JSON encode "corrupted"?
Date 2021-08-08 11:43 +0200
Message-ID <in9n9dFu47pU4@mid.individual.net> (permalink)
References <seh0um$ket$1@gioia.aioe.org> <in2il0Fg6j9U1@mid.individual.net> <seh6k8$opv$1@dont-email.me>

Show all headers | View raw


Lew Pitcher:

> On Thu, 05 Aug 2021 18:41:04 +0200, Arno Welzel wrote:
> 
>> Torakiki:
>>
>>> json_encode it seems that, in the case of only one element with index = 
>>> 0, it returns an incorrect result.
>>>
>>> 	$array = [];
>>> 	$array[0] = "Some text"
>>>
>>> it should return (in Firefox 90.0.2)
>>>
>>> 	{"0": "Some text"}
>>
>> Why? $array is still just an array with one scalar element.
>>
>>> and instead returns a string like
>>>
>>> 	["Some text"]
>>
>> Which is valid json as well. This defines an array with one element.
>>
>> See: <https://www.json.org/json-en.html>
> 
> From that link, the OP's "unexpected" string

Why? The string

["some text"]

is a valid JSON array.

Also see here:

<https://jsonformatter.curiousconcept.com>
<http://json.parser.online.fr>

[...]
> The OP changed the sequential array into a sparse array by
> assigning additional elements asequentially:
>>> 	$array[101] = "Some text 2"
>>> 	$array[1234] = "Some text 3"
> 
> Because of the "sparseness" of this object, it can't be
> expressed as a JSON array, so json_encode() encoded the
> input sparse array as a JSON object.

Correct.


-- 
Arno Welzel
https://arnowelzel.de

Back to comp.lang.php | Previous | NextPrevious in thread | Next in thread | Find similar


Thread

JSON encode "corrupted"? Torakiki <torakiki.sanNIENTE@SPAMgmail.com> - 2021-08-05 17:43 +0200
  Re: JSON encode "corrupted"? Arno Welzel <usenet@arnowelzel.de> - 2021-08-05 18:41 +0200
    Re: JSON encode "corrupted"? Lew Pitcher <lew.pitcher@digitalfreehold.ca> - 2021-08-05 17:20 +0000
      Re: JSON encode "corrupted"? Arno Welzel <usenet@arnowelzel.de> - 2021-08-08 11:43 +0200
  Re: JSON encode "corrupted"? Torakiki <torakiki.sanNIENTE@SPAMgmail.com> - 2021-08-05 21:25 +0200
    Re: JSON encode "corrupted"? "J.O. Aho" <user@example.net> - 2021-08-05 21:49 +0200
    Re: JSON encode "corrupted"? Arno Welzel <usenet@arnowelzel.de> - 2021-08-08 11:44 +0200

csiph-web