1

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","identifier": " ", "birthday":"1994-05-19", "gender":"female", "email": "rpxstaging@gmail.com" }, "accessCredentials":{ "oauthToken":"1/V2UImanylettersandnumbersrnWlVS7c", "oauthTokenSecret":"i5rlettersQU-32moreVY", "scopes": "Blogger,Google Contacts,YouTube,Picasa Web,Albums,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", "value": " " } ], "preferredUsername": "rpxstaging", "emails": [ { "type": "other", "value": "rpxstaging@gmail.com" } ], "languagesSpoken": [ "en-US" ], "id":"1048numbers2048", "displayName": "John Jackson", "profileUrl": " " }, "stat": "ok" }

  • I need above jsonString to be converted to string which looks like below

String.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\", \"identifier\": \" \", \"birthday\":\"1994-05-19\", \"gender\":\"female\", \"email\": \"rpxstaging@gmail.com\" }, \"accessCredentials\":{ \"oauthToken\":\"1/V2UImanylettersandnumbersrnWlVS7c\", \"oauthTokenSecret\": \"i5rlettersQU-32moreVY\", \"scopes\": \"Blogger,Google Contacts,YouTube,Picasa Web Albums,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\", \"value\": \" \" } ], \"preferredUsername\": \"rpxstaging\", \"emails\": [ { \"type\": \"other\", \"value\": \"rpxstaging@gmail.com\" } ], \"languagesSpoken\": [ \"en-US\" ], \"id\": \"1048numbers2048\", \"displayName\": \"John Jackson\", \"profileUrl\": \" \" }, \"stat\": \"ok\" }

  • Can some one pls tell me how to convert? I am new to this JSON
Shreyas Rao B
  • 193
  • 4
  • 17
  • Possible duplicate of [Convert JS object to JSON string](http://stackoverflow.com/questions/4162749/convert-js-object-to-json-string) – thanksd Apr 05 '16 at 16:20

1 Answers1

2

If you have the above json in a text file then you can simply do:

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

The required value (second json) is just the way debugger shows it. You probably don't need escaped values as in actual string.

In debugger it would appear as:

enter image description here

Habib
  • 219,104
  • 29
  • 407
  • 436
  • for File sud i import java.io.File? If i import same i am getting error at ReadAllText saying **The method ReadAllText(String) is undefined for the type File** – Shreyas Rao B Apr 05 '16 at 15:40
  • The above code is for C#, For Java you can use `FileUtils` like `String strJson = FileUtils.readFileToString("jsonstring.txt", "utf-8");` for more details about reading file to a string variable see http://stackoverflow.com/questions/3849692/whole-text-file-to-a-string-in-java – Habib Apr 05 '16 at 15:43