I read somewhere that json with ’ ’ (instead of " " ) is invalid, is that correct?
Why would an invalid json be returned?
Is that an issue that can be resolved with another json parser?
Why do I get the Missing a name for object member? It looks to me that the names for the sent and the received json are the same (I’ve tripled checked on the original json as well they are the same).
The code you posted doesn’t do any JSON parsing, the error should be coming from somewhere else. Show the actual line of the error please, as well as the code for any C# class or struct that you’re deserializing into.
A bug, a bad request, a bad server, bad load balancer, bad conversion, bad hackers intercepting your data, bad just about anything in the 57,000 things standing between request and response.
As Praetor points out, there’s no JSON parser above. Are these errors coming from your server?
Also, as I posted in the other thread, you can speculate indefinitely about data, or you can go observe it directly:
var request = new UnityWebRequest(url, "POST");
byte[] jsonToSend = new UTF8Encoding().GetBytes(bodyJsonString);
request.uploadHandler = new UploadHandlerRaw(jsonToSend);
request.downloadHandler = new DownloadHandlerBuffer();
request.SetRequestHeader("Content-Type", "application/json");
yield return request.SendWebRequest();
if (request.isHttpError || request.isNetworkError)
{
Debug.Log(request.error);
}
var result = request.downloadHandler.text;
var jsonData = JsonUtility.FromJson<Bearer>(result);
//Do something with jsonData...
This is how the Bearer.cs class looks like:
[System.Serializable]
public class Bearer
{
public string title;
public string item_id;
public string item_name;
}
The Bearer.cs is the same class I use as jsonBody with:
private void RequestPost(){
var bearer = new Bearer();
var jsonBody = JsonUtility.ToJson(bearer);
StartCoroutine(PostJob(URL, jsonBody));
}
Maybe the issue starts with the way the jsonbody string is serialized?
To 1. Yes, that’s correct. what you got there is not valid json. Json is a subset of the javascript language and denotes just the JavaScript Object Notation, however a lot stripped down. This page describes the entirety of JSON.
To 2. Why the server you contact returns something that is not valid json? How should we know that? Is that server supposed to return json? If so, it’s a bug on the server side. Though technically a server could return whatever data it wants. That depends on your server.
To 3. Technically that could be possible, though most parsers would not accept such formatted json. So unless the server API really can not be changed / fixed I would not consider such an approach.
To 4. This is already explained by point number 1. The returned text is not valid JSON. It does not recognise your ‘key’ as a “key” because valid keys have to be put in double quotes.