0

i write a api for my games to get achievements and so on. i load the data from a webserver into unity c# over a www request. i need a array from php which contain achievements and more data. the problem is, the result is this

[
    {"ID":"1",
    "gameID":"1",
    "name":"achv1",
    "neededvalue":"50",
    "player_achievements":{
        "ID":"8",
        "achievementID":"1",
        "playerID":"9",
        "value":"",
        "completed":""
        }
    },
    {"ID":"2",
    "gameID": "1",
    "name":"achv2",
    "neededvalue":"100",
    "player_achievements":{
        "ID":"9",
        "achievementID":"2",
        "playerID":"9",
        "value":"","completed":""
        }
    }
]

the player_achievements is a child array of the head array and i need the square_brackets around the player_achievements [] because untity c# cannot convert it to an object. i search hours for finding a solution but nobody explain how. i found this link but this is not a option for me. i want the string keys and not numbers. give it a way to use the string keys as array and not as object ? i need it like so:

[
    {   "ID":"1",
        "gameID":"1",
        "name":"achv1",
        "neededvalue":"50",
        "player_achievements":[
            {   "ID":"8",
                "achievementID":"1",
                "playerID":"9",
                "value":"",
                "completed":""
            }
        ]
    },
    {
        "ID":"2",
        "gameID":"1",
        "name":"achv2",
        "neededvalue":"100",
        "player_achievements":[
            {   "ID":"9",
                "achievementID":"2",    
                "playerID":"9",
                "value":"",
                "completed":""  
            }
        ]
    }
]
5
  • So make player_achievements an array of objects? Commented Sep 26, 2018 at 22:56
  • json_decode with the array flag? Commented Sep 26, 2018 at 22:56
  • @Jeff they lost one nesting level, it's not a serialisation format problem Commented Sep 26, 2018 at 22:57
  • The problem is with the array that you're passing to json_encode(). If you show the code that creates it, we can help you fix its structure. Commented Sep 26, 2018 at 23:30
  • might wanna json_decode() then json_encode() your data. this has helped me fix alot of JSON issues. Commented Sep 27, 2018 at 0:17

2 Answers 2

0

If I am reading your question correctly, you need to make the player_achievements element an array containing the original value of that element.

<?php

$json = <<<END
[
    {"ID":"1",
    "gameID":"1",
    "name":"achv1",
    "neededvalue":"50",
    "player_achievements":{
        "ID":"8",
        "achievementID":"1",
        "playerID":"9",
        "value":"",
        "completed":""
        }
    },
    {"ID":"2",
    "gameID": "1",
    "name":"achv2",
    "neededvalue":"100",
    "player_achievements":{
        "ID":"9",
        "achievementID":"2",
        "playerID":"9",
        "value":"","completed":""
        }
    }
]
END;

$data = json_decode($json, true);

for($i=0;$i<sizeof($data);$i++)
{
    $data[$i]['player_achievements'] = [$data[$i]['player_achievements']];
}

echo json_encode($data);

This produces a structure like the one you say you need.

[
  {
    "ID": "1",
    "gameID": "1",
    "name": "achv1",
    "neededvalue": "50",
    "player_achievements": [
      {
        "ID": "8",
        "achievementID": "1",
        "playerID": "9",
        "value": "",
        "completed": ""
      }
    ]
  },
  {
    "ID": "2",
    "gameID": "1",
    "name": "achv2",
    "neededvalue": "100",
    "player_achievements": [
      {
        "ID": "9",
        "achievementID": "2",
        "playerID": "9",
        "value": "",
        "completed": ""
      }
    ]
  }
]
Sign up to request clarification or add additional context in comments.

Comments

-2

I found an answer, I add a zero before the child element as key of array.

Comments

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.