0

I have the below json as a text, how can i assign this text into a string? Since this has got multiple inverted commas in it. Escasping each comma becomes a tedious job. I need to assign the text to string json=""; and pass a player pref string in unity. For a later purpose , so that i can later on write the string by using System.IO.File.WriteAllText(Application.persistentDataPath + "/stickers.json", json);

{
  "android_play_store_link": "adityaspt",
  "ios_app_store_link": "",
  "sticker_packs": [
    {
      "identifier": "1",
      "name": "Adi",
      "publisher": "Jane Doe",
      "tray_image_file": "Trayicon_Cat1.png",
      "image_data_version":"1",
      "avoid_cache":false,
      "publisher_email":"",
      "publisher_website": "",
      "privacy_policy_website": "",
      "license_agreement_website": "",
      "stickers": [
        {
          "image_file": "Formidable.webp",
          "emojis": ["☕",""]
        },
        {
          "image_file": "Awful.webp",
          "emojis": ["",""]
        },
        {
          "image_file": "Athletic.webp",
          "emojis": ["☕",""]
        }

      ]
    }
  ]
}
Aditya Patil
  • 123
  • 1
  • 1
  • 9
  • If not a string, what type is it now? Please show us your code. You should be able to save your json and then fetch it and deserialize it with `JsonUtility.FromJson()` – Fredrik Schön Jun 10 '20 at 16:14
  • Json is a string with specific convention (key-value-pair) so it can uniformly be used over different platform. When your json comes from the request of the PlayerPrefs, it will be a string. – Everts Jun 10 '20 at 16:24
  • @FredrikSchön I solved the issue. I just put the string string in a single line and used ' \ ' to escape the double quotes. – Aditya Patil Jun 11 '20 at 06:47
  • @Everts Any Json doesnt needs to be properly indented right ? Prettify is not required to desrialize a json ? – Aditya Patil Jun 11 '20 at 06:48

1 Answers1

1

As answered here you can use @ in front of a string to automatically escape characters that usually cause issues. However you need to double up on your quotes for obvious reasons.

For example:

string quote = @"This is an example quote where someone could say ""Hello, World!"" without having to escape using \."

Which will output (backslash included):

This is an example quote where someone could say "Hello, World!" without having to escape using .