- Home /
Json data isn't being parsed
I have the following JSON data and this is how it appears in the Visual Studio watch window:
"{\"TestResponse\":\"[{isValid:false,errorMessage:\'\',mainPath:~/yourName/yourfolder/,fileName:\'tempFile.txt\'}]\"}"
So I made two classes:
[System.Serializable]
public class ResponseData
{
public bool isValid;
public string errorMessage;
public string mainPath;
public string fileName;
}
[System.Serializable]
public class ResponseDataList
{
public List<ResponseData> TestResponse;
}
And here's how I'm parsing it:
ResponseDataList responseDataList = JsonUtility.FromJson<ResponseDataList >(jsonData);
I'm always getting zero elements in the responseDataList
though. I tried changing the bool to a string, and the errorMessage to a character (because it looks like it an empty character '\') but no luck.
Hi, try with this JSON string
string jsonData = "{ \"TestResponse\": [{ \"isValid\": true, \"error$$anonymous$$essage\": null, \"mainPath\": \"path\", \"fileName\": \"fname\" } ]}";
Nope I still get zero elements when using that :(
Ok I'm not sure if Unity's JSON can parse custom class objects (ResponseDataList in this case). Perhaps you will need to use $$anonymous$$iniJSON or NewtonJSON
EDIT: @TreyH is correct. Remove $$anonymous$$onobehaviour and use the jsonData I mentioned.
I don't think you can deserialize $$anonymous$$onoBehaviour instances...
Woops, I got rid of that. Still same issue though. I'll edit my post.
I'm not getting any weird behavior:
void Start()
{
var data = new ResponseData();
data.isValid = true;
data.error$$anonymous$$essage = "Error!";
data.mainPath = "../$$anonymous$$ain";
data.fileName = "file.txt";
var list = new ResponseDataList();
list.TestResponse = new List<ResponseData>();
list.TestResponse.Add(this.data);
var json = JsonUtility.ToJson(list);
var listDeserialized = JsonUtility.FromJson<ResponseDataList >(json);
Debug.Log(listDeserialized.TestResponse.Count);
Debug.Log(listDeserialized.TestResponse[0].mainPath);
}
prints:
1
../$$anonymous$$ain
as expected. How are you serializing it?
Answer by Bunny83 · Mar 11, 2020 at 01:55 PM
Well, like ranch000 pointed out your json is not valid json at all. The keys and also string values always have to be double quoted. No other quotes are allowed. Here's a full description of the json format. Your string actually does represent valid json, but not what you think it contains. All you got is an object with a single key-value pair. Without all the backslash escaping your json looks like this:
{
"TestResponse": "[{isValid:false,errorMessage:'',mainPath:~/yourName/yourfolder/,fileName:'tempFile.txt'}]"
}
So from the json's point of view TestResponse is just a string, nothing else. The content of that string is not valid json, even when it wasn't put into double quotes.
Like ranch000 said your actual json text should look like this
{
"TestResponse": [
{
"isValid":false,
"errorMessage":"",
"mainPath":"~/yourName/yourfolder/",
"fileName":"tempFile.txt"
}
]
}
With backslash escaping and without the whitespace it should look like this
// C#
string jsonText = "{ \"TestResponse\": [{ \"isValid\":false, \"errorMessage\":\"\", \"mainPath\":\"~/yourName/yourfolder/\", \"fileName\":\"tempFile.txt\" }]}";
This should parse correctly with the classes you have there. If you got your "json" from a web service, the issue is most likely in that web service as the returned data is not in the format you expected.
Thank you for the detailed explanation! I thought something looked weird with the format, but I never had a problem with the web service before so thought it might be something I was doing.
Answer by ShadyProductions · Mar 11, 2020 at 01:43 PM
With JsonUtility it seems to work with this:
string jsonData = "{\"TestResponse\":[{\"isValid\":true,\"errorMessage\":\"null\",\"mainPath\":\"path\",\"fileName\":\"fname\"}]}";
Here is a formatted overview:
{
"TestResponse": [
{
"isValid": true,
"errorMessage": "null",
"mainPath": "path",
"fileName": "fname"
}
]
}
Your answer
