本文介绍了畸形的JSON的语法异常的android的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是服务器响应

  [{书签= {},评论= [],createdOn = {日期= 7.0天= 5.0小时= 14.0,分= 0.0,月= 2.0毫微秒= 0.0 ,秒= 0.0,时间= 1.3942008E12,timezoneoffset的= 0.0,年= 114.0},事件= {},fileContentType =,=文件名,档案大小= 0.0,fileUrl =,= hasFile 0.0,图像= {},isCommented = 0.0, isLike =假,isOnlyMe =假,isPolled =假,isRsvp =假,isSpamed = 0.0,ismoderated = 0.0,关键字= [],meetingId = 0.0,modifiedOn = {日期= 7.0天= 5.0小时= 14.0,分= 0.0,月= 2.0毫微秒= 0.0,秒= 0.0,时间= 1.3942008E12,timezoneoffset的= 0.0,年= 114.0} originalId = 0.0,parentId的= 0.0,parentPost =,= parentPostCreatedOn {日期= 7.0天= 5.0,小时= 14.0,分= 0.0,月= 2.0毫微秒= 0.0,秒= 0.0,时间= 1.3942008E12,timezoneoffset的= 0.0,年= 114.0} parentPostUserEmailAddress =,= parentPostUserId 0.0,parentPostUserName =,pollOptions = [],后=大家好,〜|〜|〜我们提供MMv3用户指南下面的位置〜|〜网址:HTTP://mmv3.qlc.co.in/ms/MMv3_Help/MailServe_v3.00.html~ | ~~ | 〜请向我们提供您的建议和反馈,帖子ID = 39629.0,postShareWithId = [],postType = 1.0,postUserId = 100021.0,postVisibility = 0.0,selectedPollOption = 0.0,selectedRsvp = 0.0,的sourceID = 449.0,SOURCENAME =所有QLC,sourceType的= 2.0,标签= [],TAGID = [],标签=,USER = {地址=,= blogSiteUrl,证书=,=大专,connectionCount = 0.0,创建= {},说明=,emailAddress=ashwini.nimbargi@qlc。在,facebookProfile =,名字=阿什维尼,groupCount = 0.0,highestDegree =,ID = 0.0,姓氏= Nimbargi,linkedInProfile =,locationId = 0.0,移动电话号码= 0.0,修改= {},昵称=阿什维尼Nimbargi,的OtherPhone = 0.0,密码=,销code = 0.0,previousEmployer =,twitterProfile =,键入= 0.0,大学=,userCommunityId = 0.0,用户id = 100021.0,用户preference = {},网站=}的用户文件= { },视频= {},wallPostSummary = {commentCount = 0.0,likeCount = 4.0,modifiedOn = {日期= 7.0天= 5.0小时= 14.0,分= 0.0,月= 2.0毫微秒= 0.0,秒= 0.0,时间= 1.3942008E12,timezoneoffset的= 0.0,年= 114.0} =帖子ID 39629.0,spamCount = 0.0,wallPostSummaryId = 29777.0}},{书签= {},评论= [],createdOn = {日期= 7.0天= 5.0小时= 12.0,分= 44.0,月= 2.0毫微秒= 0.0,秒= 31.0,时间= 1.394196271E12,timezoneoffset的= 0.0,年= 114.0},事件= {},fileContentType =,=文件名,档案大小= 0.0,fileUrl = ,hasFile = 0.0,图像= {},isCommented = 0.0,isLike =假,isOnlyMe =假,isPolled =假,isRsvp =假,isSpamed = 0.0,ismoderated = 0.0,关键字= [],meetingId = 0.0,modifiedOn = {日期= 7.0天= 5.0小时= 12.0,分= 44.0,月= 2.0毫微秒= 0.0,秒= 31.0,时间= 1.394196271E12,timezoneoffset的= 0.0,年= 114.0} originalId = 0.0,parentId的= 0.0, parentPost =,= parentPostCreatedOn {日期= 7.0天= 5.0小时= 12.0,分= 44.0,月= 2.0毫微秒= 0.0,秒= 31.0,时间= 1.394196271E12,timezoneoffset的= 0.0,年= 114.0},parentPostUserEmailAddress = ,parentPostUserId = 0.0,parentPostUserName =,pollOptions = [],后= T,帖子ID = 39627.0,postShareWithId = [],postType = 1.0,postUserId = 100220.0,postVisibility = 0.0,selectedPollOption = 0.0,selectedRsvp = 0.0,的sourceID = 449.0, SOURCENAME =所有QLC,sourceType的= 2.0,标签= [],TAGID = [],标签=,用户= {地址=,blogSiteUrl =,证书=,大专=,connectionCount = 0.0,创建= {},说明=,EMAILADDRESS [email protected],facebookProfile =,名字=的Pankaj,groupCount = 0.0,highestDegree =,ID = 0.0,姓氏=夏尔马,linkedInProfile =,= locationId 0.0,移动电话号码= 0.0,修改= {},昵称=潘卡夏尔马的OtherPhone = 0.0,密码=,销code = 0.0,previousEmployer =,twitterProfile =,键入= 0.0,大学=,userCommunityId = 0.0,用户id = 100220.0,用户preference = {},网站=}的用户文件= {},视频= {},wallPostSummary = {commentCount = 0.0,likeCount = 0.0,modifiedOn = {日期= 7.0天= 5.0小时= 12.0,分= 44.0,月= 2.0毫微秒= 0.0,秒= 31.0,时间= 1.394196271E12,timezoneoffset的= 0.0,年= 114.0} =帖子ID 39627.0,spamCount = 0.0,wallPostSummaryId = 29775.0}}]

当我尝试解析它,我得到的异常

  com.google.gson.JsonSyntaxException:com.google.gson.stream.MalformedJsonException:第1行198列的值意外

这是我的Java code

  Log.e(TAG,fromJson.getResponse()的getResult()的toString());            WallPost [] fromJson2 = gson.fromJson(fromJson.getResponse()的getResult()的toString(),WallPost []类。);
//
            Log.e(TAG,长:+ fromJson2.length);


解决方案

试试这个

  GSON GSON =新GSON();
键入collectionType =新TypeToken<收集和LT; WallPost>方式>(){}的getType();
收集和LT; WallPost> collObj = gson.fromJson(gson.toJson(obj.Data),collectionType);
清单< WallPost> LST =新的ArrayList< WallPost>(collObj);

这是指:

TOC-阵列的例子

this is the server response

[{bookmark={}, comments=[], createdOn={date=7.0, day=5.0, hours=14.0, minutes=0.0, month=2.0, nanos=0.0, seconds=0.0, time=1.3942008E12, timezoneOffset=0.0, year=114.0}, event={}, fileContentType=, fileName=, fileSize=0.0, fileUrl=, hasFile=0.0, image={}, isCommented=0.0, isLike=false, isOnlyMe=false, isPolled=false, isRsvp=false, isSpamed=0.0, ismoderated=0.0, keywords=[], meetingId=0.0, modifiedOn={date=7.0, day=5.0, hours=14.0, minutes=0.0, month=2.0, nanos=0.0, seconds=0.0, time=1.3942008E12, timezoneOffset=0.0, year=114.0}, originalId=0.0, parentId=0.0, parentPost=, parentPostCreatedOn={date=7.0, day=5.0, hours=14.0, minutes=0.0, month=2.0, nanos=0.0, seconds=0.0, time=1.3942008E12, timezoneOffset=0.0, year=114.0}, parentPostUserEmailAddress=, parentPostUserId=0.0, parentPostUserName=, pollOptions=[], post=Hi All,~|~~|~We have provided MMv3 User Guide on below location~|~URL: http://mmv3.qlc.co.in/ms/MMv3_Help/MailServe_v3.00.html~|~~|~Please provide us your suggestions and feedback., postId=39629.0, postShareWithId=[], postType=1.0, postUserId=100021.0, postVisibility=0.0, selectedPollOption=0.0, selectedRsvp=0.0, sourceId=449.0, sourceName=All QLC, sourceType=2.0, tag=[], tagId=[], tags= , user={address=, blogSiteUrl=, certificates=, college=, connectionCount=0.0, created={}, description=, [email protected], facebookProfile=, firstName=Ashwini, groupCount=0.0, highestDegree=, id=0.0, lastName=Nimbargi, linkedInProfile=, locationId=0.0, mobileNumber=0.0, modified={}, nickName=Ashwini Nimbargi, otherPhone=0.0, password=, pinCode=0.0, previousEmployer=, twitterProfile=, type=0.0, university=, userCommunityId=0.0, userId=100021.0, userPreference={}, website=}, userFile={}, video={}, wallPostSummary={commentCount=0.0, likeCount=4.0, modifiedOn={date=7.0, day=5.0, hours=14.0, minutes=0.0, month=2.0, nanos=0.0, seconds=0.0, time=1.3942008E12, timezoneOffset=0.0, year=114.0}, postId=39629.0, spamCount=0.0, wallPostSummaryId=29777.0}}, {bookmark={}, comments=[], createdOn={date=7.0, day=5.0, hours=12.0, minutes=44.0, month=2.0, nanos=0.0, seconds=31.0, time=1.394196271E12, timezoneOffset=0.0, year=114.0}, event={}, fileContentType=, fileName=, fileSize=0.0, fileUrl=, hasFile=0.0, image={}, isCommented=0.0, isLike=false, isOnlyMe=false, isPolled=false, isRsvp=false, isSpamed=0.0, ismoderated=0.0, keywords=[], meetingId=0.0, modifiedOn={date=7.0, day=5.0, hours=12.0, minutes=44.0, month=2.0, nanos=0.0, seconds=31.0, time=1.394196271E12, timezoneOffset=0.0, year=114.0}, originalId=0.0, parentId=0.0, parentPost=, parentPostCreatedOn={date=7.0, day=5.0, hours=12.0, minutes=44.0, month=2.0, nanos=0.0, seconds=31.0, time=1.394196271E12, timezoneOffset=0.0, year=114.0}, parentPostUserEmailAddress=, parentPostUserId=0.0, parentPostUserName=, pollOptions=[], post=t, postId=39627.0, postShareWithId=[], postType=1.0, postUserId=100220.0, postVisibility=0.0, selectedPollOption=0.0, selectedRsvp=0.0, sourceId=449.0, sourceName=All QLC, sourceType=2.0, tag=[], tagId=[], tags= , user={address=, blogSiteUrl=, certificates=, college=, connectionCount=0.0, created={}, description=, [email protected], facebookProfile=, firstName=Pankaj, groupCount=0.0, highestDegree=, id=0.0, lastName=Sharma, linkedInProfile=, locationId=0.0, mobileNumber=0.0, modified={}, nickName=Pankaj Sharma, otherPhone=0.0, password=, pinCode=0.0, previousEmployer=, twitterProfile=, type=0.0, university=, userCommunityId=0.0, userId=100220.0, userPreference={}, website=}, userFile={}, video={}, wallPostSummary={commentCount=0.0, likeCount=0.0, modifiedOn={date=7.0, day=5.0, hours=12.0, minutes=44.0, month=2.0, nanos=0.0, seconds=31.0, time=1.394196271E12, timezoneOffset=0.0, year=114.0}, postId=39627.0, spamCount=0.0, wallPostSummaryId=29775.0}}]

when i try to parse it i get the exception

com.google.gson.JsonSyntaxException: com.google.gson.stream.MalformedJsonException: Unexpected value at line 1 column 198

this is my java code

Log.e(TAG, fromJson.getResponse().getResult().toString());

            WallPost[] fromJson2 = gson.fromJson(fromJson.getResponse().getResult().toString(), WallPost[].class);
//
            Log.e(TAG, "length: "+fromJson2.length);
解决方案

try this

Gson gson = new Gson();
Type collectionType = new TypeToken<Collection<WallPost>>(){}.getType();
Collection<WallPost> collObj = gson.fromJson(gson.toJson( obj.Data), collectionType);
List<WallPost> lst = new ArrayList<WallPost>(collObj);

refer from : https://sites.google.com/site/gson/gson-user-guide#TOC-Array-Examples

这篇关于畸形的JSON的语法异常的android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-28 10:20