Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用JAVA从JSON字符串对象中删除\n_Java_Json - Fatal编程技术网

如何使用JAVA从JSON字符串对象中删除\n

如何使用JAVA从JSON字符串对象中删除\n,java,json,Java,Json,我正在处理twitter数据,其中有很多特殊字符,我可以对它们进行解码。但当到达新行时,即\n,面临以下错误 Exception in thread "Twitter_StreamThread-1" org.json.JSONException: Unterminated string at 4911 [character 4912 line 1] 尝试使用REGEX、replaceAll、pattern matcher解决问题,但没有任何结果 无法解析的示例JSON对象。

我正在处理twitter数据,其中有很多特殊字符,我可以对它们进行解码。但当到达新行时,即\n,面临以下错误

Exception in thread "Twitter_StreamThread-1" org.json.JSONException: Unterminated string at 4911 [character 4912 line 1]
尝试使用REGEX、replaceAll、pattern matcher解决问题,但没有任何结果

无法解析的示例JSON对象。

{"[0].created_at":"Sat May 01 06:11:21  0000 2021","[0].id":1388375470341644292,"[0].id_str":"1388375470341644292","[0].full_text":"RT @amirs6500: Sharjeel Usmani is being targeted by sanghis 4 calling a spade a spade. People indulged in Riots, genocides, rapes, mass mur…","[0].truncated":false,"[0].display_text_range[0]":0,"[0].display_text_range[1]":140,"[0].entities.hashtags":[],"[0].entities.symbols":[],"[0].entities.user_mentions[0].screen_name":"amirs6500","[0].entities.user_mentions[0].name":"NADEEM SHAIKH","[0].entities.user_mentions[0].id":745956780119834624,"[0].entities.user_mentions[0].id_str":"745956780119834624","[0].entities.user_mentions[0].indices[0]":3,"[0].entities.user_mentions[0].indices[1]":13,"[0].entities.urls":[],"[0].metadata.iso_language_code":"en","[0].metadata.result_type":"recent","[0].source":" a href \"http:\/\/twitter.com\/download\/android\" rel \"nofollow\" Twitter for Android \/a ","[0].in_reply_to_status_id":null,"[0].in_reply_to_status_id_str":null,"[0].in_reply_to_user_id":null,"[0].in_reply_to_user_id_str":null,"[0].in_reply_to_screen_name":null,"[0].user.id":1373724258640097280,"[0].user.id_str":"1373724258640097280","[0].user.name":"Arbaz Khan","[0].user.screen_name":"Akhan6474","[0].user.location":"Mira Road, Mumbai","[0].user.description":"","[0].user.url":null,"[0].user.entities.description.urls":[],"[0].user.protected":false,"[0].user.followers_count":0,"[0].user.friends_count":12,"[0].user.listed_count":0,"[0].user.created_at":"Sun Mar 21 19:52:50  0000 2021","[0].user.favourites_count":4,"[0].user.utc_offset":null,"[0].user.time_zone":null,"[0].user.geo_enabled":false,"[0].user.verified":false,"[0].user.statuses_count":10,"[0].user.lang":null,"[0].user.contributors_enabled":false,"[0].user.is_translator":false,"[0].user.is_translation_enabled":false,"[0].user.profile_background_color":"F5F8FA","[0].user.profile_background_image_url":null,"[0].user.profile_background_image_url_https":null,"[0].user.profile_background_tile":false,"[0].user.profile_image_url":"http:\/\/pbs.twimg.com\/profile_images\/1373724420523487232\/Dbf76rdD_normal.jpg","[0].user.profile_image_url_https":"https:\/\/pbs.twimg.com\/profile_images\/1373724420523487232\/Dbf76rdD_normal.jpg","[0].user.profile_link_color":"1DA1F2","[0].user.profile_sidebar_border_color":"C0DEED","[0].user.profile_sidebar_fill_color":"DDEEF6","[0].user.profile_text_color":"333333","[0].user.profile_use_background_image":true,"[0].user.has_extended_profile":true,"[0].user.default_profile":true,"[0].user.default_profile_image":false,"[0].user.following":false,"[0].user.follow_request_sent":false,"[0].user.notifications":false,"[0].user.translator_type":"none","[0].user.withheld_in_countries":[],"[0].geo":null,"[0].coordinates":null,"[0].place":null,"[0].contributors":null,"[0].retweeted_status.created_at":"Sat May 01 05:46:56  0000 2021","[0].retweeted_status.id":1388369324117692420,"[0].retweeted_status.id_str":"1388369324117692420","[0].retweeted_status.full_text":"Sharjeel Usmani is being targeted by sanghis 4 calling a spade a spade. People indulged in Riots, genocides, rapes, mass murdering across the nation, doesn’t have any morality in teaching lessons of humanity for activist like @SharjeelUsmani #StandWithSharjeelUsmani","[0].retweeted_status.truncated":false,"[0].retweeted_status.display_text_range[0]":0,"[0].retweeted_status.display_text_range[1]":266,"[0].retweeted_status.entities.hashtags[0].text":"StandWithSharjeelUsmani","[0].retweeted_status.entities.hashtags[0].indices[0]":242,"[0].retweeted_status.entities.hashtags[0].indices[1]":266,"[0].retweeted_status.entities.symbols":[],"[0].retweeted_status.entities.user_mentions[0].screen_name":"SharjeelUsmani","[0].retweeted_status.entities.user_mentions[0].name":"Sharjeel Usmani","[0].retweeted_status.entities.user_mentions[0].id":1049960593,"[0].retweeted_status.entities.user_mentions[0].id_str":"1049960593","[0].retweeted_status.entities.user_mentions[0].indices[0]":226,"[0].retweeted_status.entities.user_mentions[0].indices[1]":241,"[0].retweeted_status.entities.urls":[],"[0].retweeted_status.metadata.iso_language_code":"en","[0].retweeted_status.metadata.result_type":"recent","[0].retweeted_status.source":" a href \"http:\/\/twitter.com\/download\/android\" rel \"nofollow\" Twitter for Android \/a ","[0].retweeted_status.in_reply_to_status_id":null,"[0].retweeted_status.in_reply_to_status_id_str":null,"[0].retweeted_status.in_reply_to_user_id":null,"[0].retweeted_status.in_reply_to_user_id_str":null,"[0].retweeted_status.in_reply_to_screen_name":null,"[0].retweeted_status.user.id":745956780119834624,"[0].retweeted_status.user.id_str":"745956780119834624","[0].retweeted_status.user.name":"NADEEM SHAIKH","[0].retweeted_status.user.screen_name":"amirs6500","[0].retweeted_status.user.location":"Navi Mumbai, India","[0].retweeted_status.user.description":"NADEEM SHAIKH\n Dj\n4th may\ntarus","[0].retweeted_status.user.url":null,"[0].retweeted_status.user.entities.description.urls":[],"[0].retweeted_status.user.protected":false,"[0].retweeted_status.user.followers_count":63,"[0].retweeted_status.user.friends_count":140,"[0].retweeted_status.user.listed_count":0,"[0].retweeted_status.user.created_at":"Thu Jun 23 12:28:50  0000 2016","[0].retweeted_status.user.favourites_count":880,"[0].retweeted_status.user.utc_offset":null,"[0].retweeted_status.user.time_zone":null,"[0].retweeted_status.user.geo_enabled":true,"[0].retweeted_status.user.verified":false,"[0].retweeted_status.user.statuses_count":271,"[0].retweeted_status.user.lang":null,"[0].retweeted_status.user.contributors_enabled":false,"[0].retweeted_status.user.is_translator":false,"[0].retweeted_status.user.is_translation_enabled":false,"[0].retweeted_status.user.profile_background_color":"F5F8FA","[0].retweeted_status.user.profile_background_image_url":null,"[0].retweeted_status.user.profile_background_image_url_https":null,"[0].retweeted_status.user.profile_background_tile":false,"[0].retweeted_status.user.profile_image_url":"http:\/\/pbs.twimg.com\/profile_images\/745962980748099586\/zK6--YPG_normal.jpg","[0].retweeted_status.user.profile_image_url_https":"https:\/\/pbs.twimg.com\/profile_images\/745962980748099586\/zK6--YPG_normal.jpg","[0].retweeted_status.user.profile_banner_url":"https:\/\/pbs.twimg.com\/profile_banners\/745956780119834624\/1466686403","[0].retweeted_status.user.profile_link_color":"1DA1F2","[0].retweeted_status.user.profile_sidebar_border_color":"C0DEED","[0].retweeted_status.user.profile_sidebar_fill_color":"DDEEF6","[0].retweeted_status.user.profile_text_color":"333333","[0].retweeted_status.user.profile_use_background_image":true,"[0].retweeted_status.user.has_extended_profile":true,"[0].retweeted_status.user.default_profile":true,"[0].retweeted_status.user.default_profile_image":false,"[0].retweeted_status.user.following":false,"[0].retweeted_status.user.follow_request_sent":false,"[0].retweeted_status.user.notifications":false,"[0].retweeted_status.user.translator_type":"none","[0].retweeted_status.user.withheld_in_countries":[],"[0].retweeted_status.geo":null,"[0].retweeted_status.coordinates":null,"[0].retweeted_status.place":null,"[0].retweeted_status.contributors":null,"[0].retweeted_status.is_quote_status":false,"[0].retweeted_status.retweet_count":1,"[0].retweeted_status.favorite_count":1,"[0].retweeted_status.favorited":false,"[0].retweeted_status.retweeted":false,"[0].retweeted_status.lang":"en","[0].is_quote_status":false,"[0].retweet_count":1,"[0].favorite_count":0,"[0].favorited":false,"[0].retweeted":false,"[0].lang":"en","[0].risk_events_keyword_id":34,"[0].keyword_id":7,"[0].risk_events_catergory":"Civil%20Unrest","[0].keywords":"Riots"}
整个对象将被视为字符串,并尝试在java类中以下面的方式进行解析,但导致了相同的错误

String jsonStr=jsonflatter.flatte(结果).replaceAll(“\n”,“\\n”)


我将在哪里错误地分析此“\n”。我参考了几篇文章和博客,它们声明将\n替换为\n。但为什么在这种JSON对象中不起作用呢。我错过了什么。如果解决了这一问题,将非常有帮助。

是否可以使用
jsonStr=jsonStr.replace(“\n”,“\\n”)?我也试过了,即使不起作用@还有其他可能的方法吗??我在eclipse中尝试过,但eclipse在每个键和值的双引号之前自动插入\以便在那里工作。但我应该在replace函数不起作用的命令提示符下执行这段代码。非常感谢您的回复,问题已经解决。实际上,错误发生在主数组对象的\u0000处。所以我用replaceAll(“\\\\u0000”),清除了这一点;