Load Specific Data from all the user PlayerPrefs on device, C#

Hello, I was hoping somebody could help.

I’m trying (and failing) to find a way to load specific data from all the user PlayerPrefs saved on the device.

So for example I have a form in my app that submits the data collected for the ‘Name’ field and saves it to PlayerPrefs, that’s fine, however I now need to load every ‘Name’ data ever submitted on that device.

So i’m thinking something like a ‘foreach Name in PlayerPrefs’ loop may be what’s needed but i’m really not sure, like I said any help would be great.

Thank you.

If you have multiple data, i suggest you to use the serialization with the json, it’s more optimized fot things like that

How about creating a class which sole purpose is to act as the class to remap the json you save in your playerprefs? Use an array or list for your player names and remap and resave when you want to update it.

Thanks for the replies both, I decided to go down the Json route, I have it writing and reading well however I’m just trying to get it to loop and output data from a specific key, so for example my json file looks like this:

{“name”:“Paul”,“number”:“165”,}
{“name”:“Ian”,“number”:“193”}

and all I want to do it loop through each entry and output the name key value so in this case Paul and Ian, any idea how I would do this? itemData[“name”] works fine but will only get the first entry as it’s not a loop.

Thanks in advance.

can you post some code for example?

Sure thing, so the script i’m using to read my Json file looks like this:

using UnityEngine;
using System.Collections;
using System.IO;
using UnityEngine.UI;
using LitJson;


public class ReadJson : MonoBehaviour {

    private string jsonString;
    private JsonData itemData;

    // Use this for initialization
    void Start () {
        jsonString = File.ReadAllText(Application.dataPath + "/Scripts/User.json");
        itemData = JsonMapper.ToObject(jsonString);

        Debug.Log(itemData["name"]);
    }
}

And my Json file looks like this:

{“name”:“Paul”,“number”:“653”}
{“name”:“Ian”,“number”:“196”}
{“name”:“Mike”,“number”:“432”}

So at the moment I can see the script is reading the file okay because if I write Debug.Log(itemData["name"]); it will output the first ‘name’ value inside the file, however what i’m after is a way to loop through each of the entries to get each ‘name’ value.

first: are you sure the json is currenlty loaded? try to debug all the itemData name
second: if you want to loop all the members in the Json Data, if it’s an array or a list you need to search foreach itemData in JsonData

foreach(JsonData _data in itemsData)
{
//compare all the _data
}

Great, thanks for the help RoMax92.

1 Like