下午好,我花了整整一个上午的时间试图使用JsonHelper类将JSON传递给数组,但没有成功。让我们看看是否有人扔电缆查看故障所在。明确说明我是在Unity中制作游戏(以防Unity是限制代码或某些内容的游戏)。
事实是,我正在尝试与Firebase数据库中的球员和得分进行排名。
如下面在WWW类中发送的路由所示,返回www的文本是下面的json。
public void Ranking ()
{
string url = "https://example.firebaseio.com/.json";
WWW www = new WWW (url);
StartCoroutine (WaitForRequest (www));
}
IEnumerator WaitForRequest (WWW www)
{
yield return www;
// check for errors
if (www.error == null)
{
User[] users;
users = JsonHelper.FromJson <User> (www.text);
Debug.Log ("WWW Ok !:" + www.text);
}
else
{
Debug.Log ("WWW Error:" + www.error);
}
}
这是返回的字符串
{"Eduardito": {"playerName": "Eduardito", "playerScore": "4"}, "Joel": {"playerName": "Joel", "playerScore": "4"}, "Karla": { "playerName": "Karla", "playerScore": "5"}, "Miguelin": {"playerName": "Miguelin", "playerScore": "4"}}
到目前为止,使用JsonHelper类将其传递给数组时,情况一团糟。它一直返回null
。我不知道这是因为在用户的类中是否存在错误。有谁能想到的东西?谢谢!对不起,我的英语不好
编辑:用户类仅具有构造函数以及名称和分数的两个变量,仅此而已。
我将JsonHelper类放入
public static class JsonHelper
{
public static User[] FromJson<User>(string json)
{
Wrapper<User> wrapper = JsonUtility.FromJson<Wrapper<User>>(json);
Debug.Log(wrapper.Items);
return wrapper.Items;
}
public static string ToJson<User> (User[] array)
{
Wrapper<User> wrapper = new Wrapper<User> ();
wrapper.Items = array;
return JsonUtility.ToJson(wrapper);
}
public static string ToJson <User>(User[] array, bool prettyPrint)
{
Wrapper <User> wrapper = new Wrapper<User>();
wrapper.Items = array;
return JsonUtility.ToJson(wrapper, prettyPrint);
}
internal static string ArrayToJsonString<T>(User[] res, bool v)
{
throw new NotImplementedException();
}
[Serializable]
private class Wrapper<User>
{
public User[] Items;
}
}
答案 0 :(得分:0)
好的,让我们从头开始。从WWW请求返回的JSON不是数组,而是一个对象。因此,您应该尝试将请求返回的格式更改为类似以下内容:
wait:-1, Object (java.lang)
parkFor$:2137, Thread (java.lang)
park:358, Unsafe (sun.misc)
park:190, LockSupport (java.util.concurrent.locks)
await:2059, AbstractQueuedSynchronizer$ConditionObject (java.util.concurrent.locks)
take:442, LinkedBlockingQueue (java.util.concurrent)
gatherAnyResult:83, InteractionResultsHandler (androidx.test.espresso)
gatherAnyResult:52, InteractionResultsHandler (androidx.test.espresso)
waitForAndHandleInteractionResults:314, ViewInteraction (androidx.test.espresso)
check:300, ViewInteraction (androidx.test.espresso)
testProgressIndicator:76, MyFragmentTest (com.my.test) <<< ***********************
invoke:-1, Method (java.lang.reflect)
runReflectiveCall:50, FrameworkMethod$1 (org.junit.runners.model)
run:12, ReflectiveCallable (org.junit.internal.runners.model)
invokeExplosively:47, FrameworkMethod (org.junit.runners.model)
evaluate:17, InvokeMethod (org.junit.internal.runners.statements)
evaluate:80, RunBefores (androidx.test.internal.runner.junit4.statement)
evaluate:531, ActivityTestRule$ActivityStatement (androidx.test.rule)
evaluate:20, RunRules (org.junit.rules)
runLeaf:325, ParentRunner (org.junit.runners)
runChild:78, BlockJUnit4ClassRunner (org.junit.runners)
runChild:57, BlockJUnit4ClassRunner (org.junit.runners)
run:290, ParentRunner$3 (org.junit.runners)
schedule:71, ParentRunner$1 (org.junit.runners)
runChildren:288, ParentRunner (org.junit.runners)
access$000:58, ParentRunner (org.junit.runners)
evaluate:268, ParentRunner$2 (org.junit.runners)
run:363, ParentRunner (org.junit.runners)
run:104, AndroidJUnit4 (androidx.test.ext.junit.runners)
runChild:128, Suite (org.junit.runners)
runChild:27, Suite (org.junit.runners)
run:290, ParentRunner$3 (org.junit.runners)
schedule:71, ParentRunner$1 (org.junit.runners)
runChildren:288, ParentRunner (org.junit.runners)
access$000:58, ParentRunner (org.junit.runners)
evaluate:268, ParentRunner$2 (org.junit.runners)
run:363, ParentRunner (org.junit.runners)
run:137, JUnitCore (org.junit.runner)
run:115, JUnitCore (org.junit.runner)
execute:56, TestExecutor (androidx.test.internal.runner)
onStart:388, AndroidJUnitRunner (androidx.test.runner)
run:2075, Instrumentation$InstrumentationThread (android.app)
现在这是一个数组!
然后,您应该确保将User类也标记为可序列化,请不要忘记!
最后但并非最不重要的一点,请记住,JsonUtility类仅适用于对象,因此它将无法反序列化数组,因为数组并非完全是对象。因此,由于Wrapper类包含名为 Items 的参数,因此JSONUtility将在给定的JSON上查找名为 Items 的参数。 您可以通过将JSONHelper.FromJson函数更改为以下内容来解决此问题:
[{"playerName": "Eduardito", "playerScore": "4"}, {"playerName": "Joel", "playerScore": "4"}]
这样,JsonUtility反序列化器将找到一个名为Items的参数,并将其正确反序列化为包装类。
这是您代码的简单版本,因此您可以更好地了解我在说什么。您可以统一复制并粘贴它,它应该可以正常工作。
Wrapper <User> wrapper = JsonUtility.FromJson <Wrapper <User >> ("{\"Items\":" + json + "}");