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?

+3


source to share


2 answers


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);

      

+6


source


Use the Value property instead of calling ToString ().

JSONNode root = JSON.Parse("{ \"Name\" : \"Joe\"}");
Console.WriteLine(root["Name"].Value);
Console.WriteLine(root["Name"].Value.Length);

      



Output:

Joe
3

      

0


source







All Articles