Retrieving unquoted string with Unity SimpleJSON
I am using SimpleJSON script from http://wiki.unity3d.com/index.php/SimpleJSON
JSONNode root = JSON.Parse ("{ \"Name\" : \"Joe\"}");
Debug.Log (root ["Name"].ToString().Length);
It returns 5
, not 3
. This is because it root ["Name"]
returns a string that is literal "Joe"
(including quotes, so 5 characters).
But this is not exactly what I wanted - I put the quotes in there because that is the only way the parser works.
Of course I could just remove the quotes manually by pulling out the substring, but I feel like this is not how it should go. So my question is, how can I get the unquoted string Joe
using this script?
source to share
The class JSONNode
is abstract; the JSONData
class method ToString()
inserts quotes. Cm:
public override string ToString ()
{
return "\"" + Escape (m_Data) + "\"";
}
Try using the property instead Value
:
JSONData root = JSON.Parse("{ \"Name\" : \"Joe\"}");
Debug.Log(root["Name"].Value.Length);
Debug.Log(root["Name"].Value);
source to share