I have some problems with converting a json string into a c# List!
This is my JSON that i get from form a server.
[
{"roundid":1,"coins":700,"created":"2016-03-16 11:13:26","duration":198,"score":765230},
{"roundid":3,"coins":330,"created":"2016-03-16 11:13:56","duration":123,"score":425726},
{"roundid":4,"coins":657,"created":"2016-03-16 11:21:23","duration":432,"score":75384},
{"roundid":8,"coins":980,"created":"2016-03-16 11:23:19","duration":271,"score":827200}
]
In my C# program i then try to transform my json string into useable objects using with this function
public List<Round> getPlayerRounds(string username)
{
string url = BaseURL + "op=findUserRounds&username=" + username;
var json = new WebClient().DownloadString(url);
RoundDataList rounds = JsonUtility.FromJson<RoundDataList>(json);
List<Round> playerRounds = new List<Round>();
//for (var i = 0; i < rounds.roundList.Count; i++)
for (var i = 0; i < rounds.roundList.Length; i++)
{
RoundData rd = rounds.roundList[i];
Round r = rd.getRound();
playerRounds.Add(r);
}
return playerRounds;
}
Here i get error
ArgumentException: JSON must represent an object type.
I've looked around and haven't found anything that works, Tried 2-3 solution even tried edited my php webservice that creates the JSON string.
My Classes looks like this
/* This is previus atempt to solve the issue
[Serializable]
public class RoundDataList
{
public List<RoundData> roundList;
}
*/
[Serializable]
public class RoundDataList
{
public RoundData[] roundList;
}
[Serializable]
public class RoundData
{
public int roundid;
public int coins;
public DateTime created;
public int duration;
public int score;
public Round getRound()
{
Round r = new Round();
r.Roundid = roundid;
r.Coins = coins;
r.Created = created;
r.Duration = duration;
r.Score = score;
return r;
}
}
Thank for looking at this long post!
Unity's new Json API does not support Json array and that is exactly what your problem is. There is a way to do it but it is a long process to repost again. Just read how to do it here.What you are looking for is the second solution that says "2. MULTIPLE DATA(ARRAY JSON)."
Creating a class and another class holding list of it works for me.
[Serializable]
public class Questions
{
public string id;
public string question;
public string answer1;
public string answer2;
}
[Serializable]
public class QuestionList
{
public List<Questions> list;
}
//and
var jstring = "{\"list\":[{\"id\":\"1\",\"question\":\"lorem Ipsome \",\"answer1\":\"yes\",\"answer2\":\"no\"},{\"id\":\"2\",\"question\":\"lorem Ipsome Sit dore iman\",\"answer1\":\"si\",\"answer2\":\"ne\"}]}";
var result = JsonUtility.FromJson<QuestionList>(jstring);
From a list, Convert to JSON
List<Questions> questionlist = new List<Questions>();
questionlist.Add(new Questions()
{
answer1 = "yes",
answer2 = "no",
id = "1",
question = "lorem Ipsome "
});
questionlist.Add(new Questions()
{
answer1 = "si",
answer2 = "ne",
id = "2",
question = "lorem Ipsome Sit dore iman"
});
var ql = new QuestionList();
ql.list = questionlist;
var result = JsonUtility.ToJson(ql);
//this gives
{"list":[{"id":"1","question":"lorem Ipsome ","answer1":"yes","answer2":"no"},{"id":"2","question":"lorem Ipsome Sit dore iman","answer1":"si","answer2":"ne"}]}
Note: "list" and questionList.list should be in same name.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With