问题
I am trying to port a piece of Code from Java to C# and I am stuck in JSon parsing. Have a look at the following Java Code
mJsonObject = new JSONObject(str);
Iterator<String> keys=mJsonObject.keys();
while(keys.hasNext()){
String key=keys.next();
String value=mJsonObject.getString(key);
mAdData.add(new AdData(key, new JSONObject(value)));
}
I had a string which has verified Json format and I passed it to JSONObject and every thing was finely working in Java, but now in C# Unity I am not able to port it successful. I am using LitJson to perform this task and I have no idea how this works. I am badly stuck please help. Thanks
回答1:
The keys method of the JSONObject class
returns an ICollection<string>
. You can iterate an ICollection
like this. So I would change your while
loop into a foreach
, like this:
foreach (string key in keys) {
//whatever
}
来源:https://stackoverflow.com/questions/27600792/working-with-litjson-in-c-sharp-in-unity