I ' mmakingrequeststoahttpserversendingjsonstring.iusedgsonforserializinganddeserializingjsonobjects.todayiobservedthispred
I have:
stringjsonasstring=gson.to JSON (jsonasobject ).replace )、(((() ) );
That outputs exactly this:
{'content':'Literal','pos':{'left':20,'top':20}}
nowi ' musingoutputstreamwriterobtainedfromhttpurlconnectiontomakehttp,putrequestwithjsonpayload.theforegoingrequestworksforksffoad
OS.write ((内容(: (轻型)、(销售点)、 (左)、20、(顶)、3336660
However,when I say:
操作系统. write (JSON asstring );
. therequestdoesn ' twork (thisserverdoesn ' treturnanyerrorsbuticanseethatwhenwritingjsonasstringjectitdoesn ' tdowhatitits oesn ' tdo what.isthereadifferencewhenusingstringliteraloverstringobject.amidoingsomethingwrong?
Here is the snippet:
公共语音构件(表面构件tsw,字符串构件id ) {
Gson gson=new Gson (;
stringjsonwidget=gson.to JSON (SW ).replace (()、() );
try {
hurl.connect (;
outputstreamwriteros=newoutputstreamwriter (hurl.get output stream ();
//OS.write ((内容(:20 ) ) literal )、() pos )、((left )、(:20、() top )、() top ) )
操作系统. write (JSON构件;
操作系统. flush (;
操作系统. close (;
}catch(ioexceptione ) {
e .打印堆栈跟踪(;
}
}
解决方案
Remove the .replace('',' instruction. It's unnecessary )。
you ' reforcedtoaddslashesbeforedoublequoteswhenyousendajsonstringliteralbecauseinajavastringliteral,doublequotesmuotestbestbescapapapppal theywouldmarktheendofthestringinsteadofbeingadoublequoteinsidethestring )。
But the actual String,in the bytecode,doesn ' tcontainthesebackslashes.they ' reonlyusedinthesourcecode。