Problem querydata with newline (\ and newline, tab ect)

1539
6
12-29-2011 05:49 AM
nicogis
MVP Frequent Contributor
I have a value in a field string example "Test /n Test1...".
If I publish a mapserver and I do a request rest query with response type json: it returns "Test //n Test1..." but from json.org for newline in string I use /n. In rest esri uses QueryData because I have tried with this method and I have same response but if I use IRecordset I have no problem.


ESRI.ArcGIS.RuntimeManager.Bind(ESRI.ArcGIS.ProductCode.Desktop);
 IRecordSet pNewRs  = new RecordSet();
 IRecordSetInit prsInit = pNewRs as IRecordSetInit;

 IFields pFields = new Fields();
 IFieldsEdit pFieldsEdit = pFields as IFieldsEdit;
 pFieldsEdit.FieldCount_2 = 2;

 IField pField = new Field();
 IFieldEdit pFieldEdit = pField as IFieldEdit;
 pFieldEdit.Name_2 = "Type";
 pFieldEdit.Type_2 = esriFieldType.esriFieldTypeString;
 pFieldEdit.Length_2 = 50;
 pFieldsEdit.set_Field(0,pField);

 pField = new Field();
 pFieldEdit = pField as IFieldEdit;

 pFieldEdit.Name_2 = "Area";
 pFieldEdit.Type_2 = esriFieldType.esriFieldTypeDouble;
 pFieldsEdit.set_Field(1,pField);

 prsInit.CreateTable(pFields);

 ICursor pIC = prsInit.Insert();
 IRowBuffer pRowBuf = prsInit.CreateRowBuffer();

 
 pRowBuf.set_Value(0, "Test /n Test1");
 pRowBuf.set_Value(1, 10.2);
 pIC.InsertRow(pRowBuf);
 

 byte[] fs = Conversion.ToJson(pNewRs);
 using (System.IO.FileStream f = new System.IO.FileStream(@"c:\test.txt", System.IO.FileMode.Create))
 {
  f.Write(fs, 0, fs.Length);
  f.Flush();
  f.Close();
 }



I have "Test \n Test1" in test.txt:
{"displayFieldName":"","fieldAliases":{"Type":"Type","Area":"Area"},"fields":[{"name":"Type","type":"esriFieldTypeString","alias":"Type","length":50},{"name":"Area","type":"esriFieldTypeDouble","alias":"Area"}],"features":[{"attributes":{"Type":"Test \n Test1","Area":10.2}}]}
0 Kudos
6 Replies
RaviNarayanan
Esri Contributor
The escape character in json is backslash  (\). I see that you have specified the newline with a forward slash as /n. Does it work if a backslash is used?

In 10.0 I get the following response for a querying a string field that has a carriage return and a newline:
{
      "attributes" : {
        "FID" : 10,
        "ID" : 12,
        "TEXTFIELD" : "new line \r\n in text",
        "TEXT_ESC" : "new line \r\n in text"
      }
    }
0 Kudos
nicogis
MVP Frequent Contributor
Thank you for replay.
Sorry I made a mistake in my writing: I meant that backslash. I have in field DENOM a string with this text: "Test \r\n Test1" but I have this result from this stardard query request (ags .net 10sp3 with data in file geodatabase): http: //myserver/ArcGIS/rest/services/myservice/MapServer/0/query?text=&geometry=&geometryType=esriGeometryPoint&inSR=&spatialRel=esriSpatialRelIntersects&relationParam=&objectIds=1&where=&time=&returnCountOnly=false&returnIdsOnly=false&returnGeometry=true&maxAllowableOffset=&outSR=&outFields=*&f=pjson

...
"features" : [
    {
      "attributes" : {
        "OBJECTID" : 1,
        "IDEXT" : "01555",
        "DENOM" : "Test \\r\\n Test1",
        "INSEGNA" : " ",
....

What's the problem? I have tested also with querydata method in arcobjects and I have same problem. no problem with IRecordset arcobjects.
0 Kudos
RaviNarayanan
Esri Contributor
Domenico,

In JSON backslash is escaped. Since the new line is represented as \n in the string the backslash is probably getting escaped with an additional \.

Ravi
0 Kudos
nicogis
MVP Frequent Contributor
I understand what you say, but:
- why did you say that it is escaped while in your previous reply in the visualization of your response it is not escaped?

- why is it that, if I follow either Querydata method or IRecordset (see my previous example) I have two different results; the first one escaped while the second not escaped?

- the specification are not followed


Thank you in advance for your help.
0 Kudos
RaviNarayanan
Esri Contributor
Domenico,

The difference is , I had created the newline in the feature class by copy-pasting a new line from notepad++ into the field value. You are representing the newline by the escaped value \n.

Is your question specific to REST API or ArcObjects?

thanks
Ravi
0 Kudos
nicogis
MVP Frequent Contributor
Ok, thank you. Copy and Paste a new line.
0 Kudos