Closed lyx32 closed 4 years ago
I don't quite understand the issue here. ToString of a JSONNode should return the json representation of that node. Doing an implicit or explicit cast to string will actually try to access the content only. This is the intended behaviour.
Your example code is a bit confusing since it won't compile in it's current state. Also you have quotes before your array and before your object which makes no sense unless you want to embed json text as string inside the outer json structure. However if your "data" should represent a json array there must not be a quote after the colon.
If you have trouble with some json text it might be your text that doesn't follow the right syntax.
My english is not very good, but you should be able to understand what i mean
demo1 `
String jsonString = "{\"id\":\"1\",\"name\":\"name-1\"}";
JSONNode j = JSON.Parse(jsonText);
String id1 = j["id"]; // return 1
String id2 = (String)j["id"]; // return 1
String id3 = j["id"].ToString(); // return "1"
String name1 = j["name"]; // return name-1
String name2 = (String)j["name"]; // return name-1
String name3 = j["name"].ToString(); // return "name-1"
`
demo2
`
using (StreamReader sr = new StreamReader("D:\\json.txt"))
{
JSONNode j = JSON.Parse(sr.ReadToEnd());
String id1 = j["id"]; // return 111
String id2 = (String)j["id"]; // return 111
String id3 = j["id"].ToString(); // return "111"
String name1 = j["name"]; // return json.text.name
String name2 = (String)j["name"]; // return json.text.name
String name3 = j["name"].ToString(); // return "json.text.name"
}
` file "json.txt " content
{"id":"111","name":"json.text.name"}
JSONNode json = JSON.Parser("{\"id\":\"1\",\"name\":\"name-1\",\"data\":\"["{\"dataId\":\"1001\"}]\"}"); String val = (String)json["id"]; //return 1 String val2 = json["id"].ToString(); // return "1"
if modify code
internal override void WriteToStringBuilder(StringBuilder aSB, int aIndent, int aIndentInc, JSONTextMode aMode) { //aSB.Append('\"').Append(Escape(m_Data)).Append('\"'); aSB.Append(Escape(m_Data)); }
so
String val = (String)json["data"]; // return "" String val2 = json["data"].ToString(); // return \"[\"{\"dataId\":\"1001\"}]\"