首页 > 编程知识 正文

outputstream转字符串(outputstream用法)

时间:2023-05-06 05:57:39 阅读:76375 作者:3601

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。

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。