0
$json=
"{
"name":"John",
"name":"Karel"
}";

In case of use this json_decode($json, true); i get the array['name']="Karel" so after parse i get the following xml:

xml:

<?xml version="1.0" encoding="UTF-8"?>
<root>
    <name value="Karel"/>
</root>

I need to get this xml:

<?xml version="1.0" encoding="UTF-8"?>
<root>
   <name>
      <element>John</element>
      <element>Karel</element>
   </name>
</root>

I think that i need my own json parser, i need to get array['name']=["John","Karel"]; then i can manage to do this.

EDITS: I can't edit input json. I need decode from file to associative array in case of same keys i need to make an array with both values on same index.

ok whole JSONfile is this:

{
    "name":"John",
    "name":"Karel",
    "heslo":{"content":"Dam ten shit podle sebe"},
    "ageA":30,
    "d":21,
    "d":42,
    "ageB":30.2,
    "ageC":1,
    "ageD":0,
    "testChar":["&","<",">","\"", "'", "-"],
    "lada":{"pritelkyne":"Kristynka","plysak":"mimon", "vek":20, "oblibenýSeriál":"RobinsonůvOstrov"},
    "null":null,
    "boolT":true,
    "boolF":false,
    "cars":[ { "type": "Skoda", "number": "212 555-1234" },"Ford", "BMW",{ "type": "Hyundai", "number": "215 555-1234" }, "Fiat" ],
    "numbersSeven":[1,2,3,4,5,6,7],
    "numbersFourteen":[1,2,3,4,5,6,7,8,9,10,11,12,13,14],
    "numbersHundredOne":[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64,65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,80,81,82,83,84,85,86,87,88,89,90,91,92,93,94,95,96,97,98,99,100,101]
}

but it is just one from the many examples, i need to do this generrally not exactly for this json.

So how should i do the parsing ?

7
  • 1
    it isn't about json_decode , you need to re-factor your array . Commented Mar 7, 2017 at 10:51
  • how should i refactor the array ? Commented Mar 7, 2017 at 10:56
  • i dont thin that refactoring array will work i dont have the values in array i lose them ehn im decoding from json file Commented Mar 7, 2017 at 11:37
  • 1
    To clarify, that JSON file is invalid, because you have the same key with two different values. If you can't change it, you need to know that most JSON parsers out there will either produce an error, or throw away part of the data (I think the spec leaves it up to the implementer what to do, but "create a new structure holding both values" is very unlikely.) Commented Mar 7, 2017 at 12:16
  • @PaulCrovella Yes, I realised that "invalid" was technically too strong a term, perhaps "extremely non-portable" would be more accurate. The RFC goes on to say "When the names within an object are not unique, the behavior of software that receives such an object is unpredictable." For all practical purposes, duplicate keys are A Bad Idea, because if you want something non-portable, just use a custom format native to your ecosystem of choice. Commented Mar 7, 2017 at 18:13

2 Answers 2

2

You can't use same key for different values. Instead of this you can store values in single array. Or use different array then store it in another key:

Try with following codes:

$json : '{"names":[{"name":"John"},{
"name":"Karel"}]
}';

OR

$json = '{"name":["John","Karel"]}';

EDIT

As edited question If you receive this input and want to change json after reeving it. You can try following code (Change json string)

$json=
'{
"name":"John","name":"Karel"
}';

$json =  ltrim($json,"{");
$json= rtrim($json,"}");
$tmp_arr = (array_map('trim', explode(',', $json)));
$new_arr['name']=array();
foreach ($tmp_arr as $key => $value) {
  $val = explode(':', $value);
  if(count($val)>1){
    $val=$val[1];
    $val = str_replace('"', '', $val);
    $new_arr['name'][] = $val;
  }

}

echo json_encode($new_arr); //{"name":["John","Karel"]}
Sign up to request clarification or add additional context in comments.

8 Comments

i think that the second example will output the desired output in the question
i can't do this, i need to do in decoding.
But it will never execute same keys. It will always take last value Because last one will replace previous value
i undrestand why is hapenning but i need to save both values as i said above
ok that cold be it could you do this for every key not just 'name' ? :-) I need to do this for whole json string generrally for every key
|
0

try to use jsonarray : it would look like [ {"name":"John"}, {"name":"Karel"} ] so try this

$arraysforjson = array();
$arraysforjson [] = array('name' => "$name")
$json = json_encode($d);

1 Comment

i need decode not encode and i need key that is in my json file i dont have only name as keys, this is just snipped json

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.