如何在没有未知密钥的情况下访问Json数据? [英] How to access Json data without unknown key?
问题描述
public static string RESTToJsonConverter(string incoming_data){
string data = "[";
int i = 0;
Debug.Log("incoming_data"+incoming_data);
data += "]";
string JSONToParse = "{\"values\":" + data + "}";
return JSONToParse;
}
下面是我运行该代码时的结果.我的问题是,如何在不使用"M4qRmfIqhKdy643Ujye"
键(自动生成)的情况下访问/获取所有数据?
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)?
如果使用JavaScript,则可以使用object.values
,但是由于使用的是C#,因此我不知道如何获取数据.
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"
},
"-M4qRmfIqhKdy643Ujye": {
"assetName": "avatar",
"id": "-M4qRmfnFya7bC43Ujye",
"imageName": "icon_avatar",
"name": "Bobfds",
"objName": "Bobbydsf",
"point": "-M4vZRY9vhKs65n5L_Gk",
"versionNumber": "3"
},
"-M4qRmfIqhKdy643Ujye": {
"assetName": "avatar",
"id": "-M4qRmfnFya7bC43Ujye",
"imageName": "icon_avatar",
"name": "Bobfdsa",
"objName": "Bobbyfc",
"point": "-M4vZRY9vhKs65n5L_Gk",
"versionNumber": "3"
}
}
更新
这是我的下载程序类中的功能之一
This 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
{
//Debug.Log(request.downloadHandler.text);
string json =
FirebaseSetup.RESTToJsonConverter(request.downloadHandler.text);
callback(json);
}
下面是我的FirebaseSetup类
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}");
// remove everything before the SECOND occurrence of '{'
// remove last occurrence of '}'
var startIndex = incoming_data.IndexOf('{', incoming_data.IndexOf('{') + 1);
var endIndex = incoming_data.LastIndexOf('}') - 1;
var json = incoming_data.Substring(startIndex, endIndex - startIndex);
// then remove leading or trailing whitespace
json = json.Trim();
Debug.Log($"json:/n{json}");
var data = JsonUtility.FromJson<string>(json);
return data;
}
尝试@derHugo代码后,出现新错误.
After I try @derHugo code, I get new error.
推荐答案
对于您的具体情况,我会反其道而行:
For your specific case I would go the other way round:
根字段名称通常无关紧要,因此,如果删除尾随的}
并从 second {
开始,则需要输入字符串
The root field name usually doesn't matter so if you remove the trailing }
and start the string from the second {
you would have
{
"assetName": "avatar",
"id": "-M4qRmfnFya7bC43Ujye",
"imageName": "icon_avatar",
"name": "Bob",
"objName": "Bobby",
"point": "-M4vZRY9vhKs65n5L_Gk",
"versionNumber": "3"
}
您可以简单地为其创建一个c#类
which you can simply create a c# class for
[Serializable]
public class Data
{
public string assetName;
public string id;
public string imageName;
public string name;
public string objName;
public string point;
public string versionNumber;
}
然后可以使用JsonUtility
public static Data RESTToJsonConverter(string incoming_data)
{
Debug.Log($"incoming_data:/n{incoming_data}");
// remove everything before the SECOND occurrence of '{'
// remove last occurrence of '}'
var startIndex = incoming_data.IndexOf('{', incoming_data.IndexOf('{') + 1);
var endIndex = incoming_data.LastIndexOf('}') - 1;
var json = incoming_data.Substring(startIndex, endIndex - startIndex);
// then remove leading or trailing whitespace
json = json.Trim();
Debug.Log($"json:/n{json}");
var data = JsonUtility.FromJson<Data>(json);
return data;
}
更新
您现在更新了问题内容,因此现在数据作为数据对象的字典出现.
Update
You now updated your question content so now the data comes as a Dictionary of data objects.
在这种情况下,您可以使用 Newtonsoft Json.NET
直接支持对Dictionary
例如
In this case you could use Newtonsoft Json.NET
which directly supports (de)serialization of Dictionary
like e.g.
[Serializable]
public class Data
{
public string assetName;
public string id;
public string imageName;
public string name;
public string objName;
public string point;
public string versionNumber;
}
然后做类似的事情
public static Dictionary<string, Data> RESTToJsonConverter(string incoming_data)
{
Debug.Log($"incoming_data:/n{incoming_data}");
var data = JsonConvert.DeserializeObject<Dictionary<string, Data>(json);
return data;
}
那么你可以做
var datas = RESTToJsonConverter(receivedRawData);
foreach(var data in data.Values)
{
Debug.Log(data.id);
}
这篇关于如何在没有未知密钥的情况下访问Json数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!