【问题标题】:How to convert JsonString to String?如何将 JsonString 转换为字符串?
【发布时间】:2016-07-25 15:08:05
【问题描述】:

JsonString.txt

{ "profile": { "name": { "formatted": "John Jackson", "givenName": "John", "familyName": "Jackson" }, "verifiedEmail": "rpxstaging@gmail.com ","googleUserId":"1048morenumbers2048","displayName":"John Jackson","photo":"","preferredUsername":"rpxstaging","url":"","providerName":"Google",标识符“:”,“生日”:“1994-05-19”,“性别”:“女性”,“电子邮件”:“rpxstaging@gmail.com”},“accessCredentials”:{“oauthToken”:“1 /V2UImanylettersandnumbersrnWlVS7c", "oauthTokenSecret":"i5rlettersQU-32moreVY", "scopes": "Blogger,Google Contacts,YouTube,Picasa Web,相册,Google Calendar,Google Docs,Google Mail,Google OpenSocial", "scope":"" , "type":"OAuth" }, "merged_poco": { "name": { "formatted": "John Jackson", "givenName": "John", "familyName": "Jackson" }, "addresses": [ { "country": "United States" } ], "photos": [ { "type": "other", "value": " " } ], "urls": [ { "type": "profile", “价值”:“”}],“首选用户名”:“rpxstaging”,“电子邮件”:[{“类型”:“其他”,“价值”:“r pxstaging@gmail.com" } ], "languagesSpoken": [ "en-US" ], "id":"1048numbers2048", "displayName": "John Jackson", "profileUrl": " " }, "stat": “好的”}

  • 我需要将上面的 jsonString 转换为如下所示的字符串

String.txt

{ \"profile\": { \"name\": { \"formatted\": \"John Jackson\", \"givenName\": \"John\", \"familyName\": \ "Jackson\" }, \"verifiedEmail\": \"rpxstaging@gmail.com\", \"googleUserId\": \"1048morenumbers2048\", \"displayName\": \"John Jackson\", \"照片\": \" \" , \"preferredUsername\": \"rpxstaging\", \"url\": \" \", \"providerName\": \"Google\", \"identifier\": \ “\”,\“生日\”:\“1994-05-19\”,\“性别\”:\“女性\”,\“电子邮件\”:\“rpxstaging@gmail.com\”},\ "accessCredentials\":{ \"oauthToken\":\"1/V2UImanylettersandnumbersrnWlVS7c\", \"oauthTokenSecret\": \"i5rlettersQU-32moreVY\", \"scopes\": \"Blogger,Google 通讯录,YouTube,Picasa网络相册、Google 日历、Google 文档、Google Mail、Google OpenSocial\" 、\"scope\": \"\"、\"type\": \"OAuth\" }、\"merged_poco\": { \" name\": { \"formatted\": \"John Jackson\", \"givenName\": \"John\", \"familyName\": \"Jackson\" }, \"addresses\": [ { \"country\": \"United States\" } ], \"photos\": [ { \"type\": \"other\", \"value\": \" " } ], \"urls\": [ { \"type\": \"profile\", \"value\": \" \" } ], \"preferredUsername\": \"rpxstaging\" , \"emails\": [ { \"type\": \"other\", \"value\": \"rpxstaging@gmail.com\" } ], \"languagesSpoken\": [ \"en- US\" ], \"id\": \"1048numbers2048\", \"displayName\": \"John Jackson\", \"profileUrl\": \" \" }, \"stat\": \"好的\" }

  • 谁能告诉我如何转换?我是这个 JSON 的新手

【问题讨论】:

标签: java c# json string file


【解决方案1】:

如果您在文本文件中有上述 json,那么您可以简单地这样做:

string strJson = File.ReadAllText("jsonstring.txt"); //Specify the path

所需的值 (second json) 只是调试器显示它的方式。您可能不需要像实际字符串中那样的转义值。

在调试器中它会显示为:

【讨论】:

  • 对于文件 sud 我导入 java.io.File?如果我导入相同的内容,我会在 ReadAllText 处收到错误,说 方法 ReadAllText(String) 未定义类型文件
  • 以上代码适用于C#,对于Java,您可以使用FileUtils,如String strJson = FileUtils.readFileToString("jsonstring.txt", "utf-8");,有关将文件读取到字符串变量的更多详细信息,请参阅stackoverflow.com/questions/3849692/…
猜你喜欢
  • 1970-01-01
  • 2015-09-17
  • 2023-03-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-01-06
  • 2011-07-11
  • 1970-01-01
相关资源
最近更新 更多