Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to turn plain text json data into string?

I am trying to mock up some test data to check wether a json string deserializes into an object correctly.

I have some json data which is 660 lines long, so i have only included a portion

{
"DataA": "string",
"DataB": "datetime",
"DataC": {
    "DataC1": "datetime",
    "DataC2": "datetime",
    "DataC3": "datetime",
    "DataC4": int,
    "DataC5": int,
    "DataC6": "string",
    "DataC7": int,
    "DataC8": "object"
},
"DataD": {
    "DataD1": decimal,
    "DataD2": decimal,
    "DataD3": "string",
    "DataD4": int,
    "DataD5": decimal,
    "DataD6": "string",
    "DataD7": {
        "DataD7i": null,
        "DataD7ii": [

I have created the corresponding classes, but am currently attempting to test them. However I am unable to get this json data into a string, as the double quotation marks close off the string. I have tried using ecsapes aswell but to no avail.

string testjson = "{
"DataA": "string",
"DataB": "datetime",
"DataC": {
    "DataC1": "datetime",
    "DataC2": "datetime",
    "DataC3": "datetime",
    "DataC4": int,
    "DataC5": int,
    "DataC6": "string",
    "DataC7": int,
    "DataC8": "object"
},
"DataD": {
    "DataD1": decimal,
    "DataD2": decimal,
    "DataD3": "string",
    "DataD4": int,
    "DataD5": decimal,
    "DataD6": "string",
    "DataD7": {
        "DataD7i": null,
        "DataD7ii": ["

I want to call

            ObjectA objectblah= JsonConvert.DeserializeObject<ObjectA>(output);

But cannot manage to get the json into a string. I am aware this is a trivial issue, but I am new and am stuck on this issue. any help would be greatly appreciated.

Thanks

like image 348
gweilo Avatar asked Dec 16 '14 02:12

gweilo


People also ask

Can I convert JSON to text?

Users can also Convert JSON File to Text by uploading the file. Download converted file with txt extension. JSON to Text Online works well on Windows, MAC, Linux, Chrome, Firefox, Edge, and Safari.

How do I convert a JSON file to readable?

If you need to convert a file containing Json text to a readable format, you need to convert that to an Object and implement toString() method(assuming converting to Java object) to print or write to another file in a much readabe format. You can use any Json API for this, for example Jackson JSON API.

How do I convert a JSON to a string in Python?

There are two ways of converting JSON to string in Python; one way is to use json method dumps(), and another way is to use an API such as requests module in Python.


2 Answers

In my unit test projects, whenever I have "mass" text, I put that content into a separate text file. Then you have two choices:

  1. Make that text file an embedded resource, which you can load via Assembly.GetExecutingAssembly().GetManifestResourceStream(...).
  2. Or set the file's project property "Copy to output directory" to "If newer". Then just read it via File.ReadAllText.

Keeping it in a separate file makes editing/maintenance a lot easier.

like image 39
Christoph Avatar answered Sep 28 '22 08:09

Christoph


Part of the issue looks to be the use of double quotes which can be escaped with a backslash \, however to have a multi-line string in C#, you also need to append an @ symbol at the start like shown in this answer https://stackoverflow.com/a/1100265/2603735

like image 80
Jayden Meyer Avatar answered Sep 28 '22 08:09

Jayden Meyer