-1

Below is my result when I run that code. My Question is how can I access/get all the data without taking "M4qRmfIqhKdy643Ujye" key (auto-generate)? I just want the {assetname, id, ... etc }. I don't want to take the auto generate id with them.

If using JavaScript, I can use object.values but since I'm using C#, I don't know how to get the data.

{
    "-M4qRmfIqhKdy643Ujye": {
        "assetName": "avatar",
        "id": "-M4qRmfnFya7bC43Ujye",
        "imageName": "icon_avatar",
        "name": "Bob",
        "objName": "Bobby",
        "point": "-M4vZRY9vhKs65n5L_Gk",
        "versionNumber": "3"
    },
    "-M4qRmfIjjd5c643Ujye": {
        "assetName": "avatar",
        "id": "-M4qRmfnFya7bC43Ujye",
        "imageName": "icon_avatar",
        "name": "Bobfds",
        "objName": "Bobbydsf",
        "point": "-M4vZRY9vhKs65n5L_Gk",
        "versionNumber": "3"
    },
    "-M4qRmfIqhKdnjdsjye": {
        "assetName": "avatar",
        "id": "-M4qRmfnFya7bC43Ujye",
        "imageName": "icon_avatar",
        "name": "Bobfdsa",
        "objName": "Bobbyfc",
        "point": "-M4vZRY9vhKs65n5L_Gk",
        "versionNumber": "3"
    }

}

Below is one of function in my Downloader Class

IEnumerator DownloadData(string dataPath, Action<string> callback){
    Debug.Log("dataPath=>"+dataPath);
    var token = LocalData.getAuth();
    Auth data = JsonUtility.FromJson<Auth>(token);
    var request = new 
    UnityWebRequest("https://test123.firebaseio.com/"+dataPath+".json? 
    auth="+data.idToken, "GET");
    request.downloadHandler = (DownloadHandler) new 
    DownloadHandlerBuffer();
    request.SetRequestHeader("Content-Type", "application/json");

    yield return request.SendWebRequest();

    if (request.isHttpError || request.isNetworkError)
    {
        Debug.Log(request.error);
        NotificationHelper.showOnePopup("Error \n"+request.error);
        callback(null);
    }
    else
    {
        string json = 
        FirebaseSetup.RESTToJsonConverter(request.downloadHandler.text);
        callback(json);
    }

Below is my FirebaseSetup Class

public static string FirebaseToJsonConverter(DataSnapshot snapshot){
    string data = "[";
    int i = 0;
    foreach(DataSnapshot s in snapshot.Children){
        data += s.GetRawJsonValue();
        i++;
        if(i != snapshot.ChildrenCount)
            data += ",";
    }
    data += "]";
    string JSONToParse = "{\"values\":" + data + "}";
    return JSONToParse;
}

public static string RESTToJsonConverter(string incoming_data)
{
    Debug.Log($"incoming_data:/n{incoming_data}");

    var startIndex = incoming_data.IndexOf('{', incoming_data.IndexOf('{') + 1);
    var endIndex = incoming_data.LastIndexOf('}') - 1;
    var json = incoming_data.Substring(startIndex, endIndex - startIndex);

    json = json.Trim();
    
    Debug.Log($"json:/n{json}");

    var data = JsonUtility.FromJson<string>(json);

    return data;
}

This is my error

Syed Bob
  • 23
  • 4
  • First of all, you know that if you duplicate the key, only the last property value will remain? `{"foobar": "one", "foobar": "two", "foobar": "three"}` gives just: `{"foobar": "three"}` So if they all have the same key, `-M4qRmfIqhKdy643Ujye` then you'll have to remap the keys with the raw JSON text somehow before parsing it as JSON. – Wyck Sep 08 '20 at 03:48
  • @Wyck that might just be sample data. If it's not, then you can't use DOM but you can still use [JsonReader](https://www.newtonsoft.com/json/help/html/T_Newtonsoft_Json_JsonReader.htm) to stream the elements – Andrew Williamson Sep 08 '20 at 04:03
  • 1
    @Wyck im sorry, actually the key is not duplicate. I copy paste but forgot to change its value. – Syed Bob Sep 08 '20 at 04:18

1 Answers1

0

One way to get the values for each of the autogenerated keys is by converting it to a Dictionary<string, Data>. When you convert it to a dictionary, you can ignore the keys and take only the values to do what you need, in a List.

For instance, example

public class Data
{
    public string assetName { get; set; }
    public string id { get; set; }
    public string imageName { get; set; }
    public string name { get; set; }
    public string objName { get; set; }
    public string point { get; set; }
    public string versionNumber { get; set; }
}

// in your main,
var obj = JsonConvert.DeserializeObject<Dictionary<string, Data>>(json);
List<Data> allData = obj.Select(x => x.Value).ToList();

allData will have a list of all the objects. You can use Linq to access each of the objects and get the properties you want.

Jawad
  • 11,028
  • 3
  • 24
  • 37