-1

I get an answer from the server in the form of such JSON:

var zohozoho_atliview92 = {\"Itinerary\":[
 {\"Client_Email\":\"garymc\",
 \"Client_Name\":\"Gary\",
 \"NT_Number\":\"NT-1237\",\"Number_of_Nights\":7,
 \"ID\":\"24297940\",
 \"Itinerary_Name\":\"Icelandnights\",
 \"Tour_Template_Name\":\"Iceland FireDrive\",
 \"Departure_Date\":\"2018-07-04\"}
]};

I need to remove this: var zohozoho_atliview92 = {\"Itinerary\":[ and delete last 3 characters ]}; to Deserialize it in my object.

How can i make it using Regular Expressions? Or is there a better variant?

Nikita Goncharuk
  • 795
  • 1
  • 8
  • 23
  • 1
    Weird. It's not *really* a JSON response. Also, are the backslashes actually part of the reponse, or did you just copy the string literal? – marsze Oct 25 '18 at 07:23
  • It's not json response. :) use postman or fiddler. If you are using browser console, try to go to network tab and see what response it gives you when you call your method – Md. Tazbir Ur Rahman Bhuiyan Oct 25 '18 at 07:25
  • Are you viewing this string in the Visual Studio debugger? That will helpfully add escapes so that it shows a string literal that can be copied into sourcecode. – Hans Kesting Oct 25 '18 at 07:26
  • Paste the original json please and not the escaped version – TheGeneral Oct 25 '18 at 07:29
  • 1
    I call an XY problem and impossible to answer without wild speculation. – TheGeneral Oct 25 '18 at 07:30
  • Given, that all the escaping is due to copy&paste and this is somehow JSON, then you can't just get rid of those chars and expect it to work. Doing so would mean you can be absolutely 100% sure that this array always and ever will contain only 1 single entry. – Fildor Oct 25 '18 at 07:39
  • Yes, it always contains one entry. – Nikita Goncharuk Oct 25 '18 at 07:42
  • The point is not in the Json format, but in the fact that I need to get rid of the extra characters, when deserialize string – Nikita Goncharuk Oct 25 '18 at 07:43
  • Is this really the full response you get, including `var zohozoho_atliview92 =` and the closing `;`? That is a javascript statement, not a valid JSON response – Hans Kesting Oct 25 '18 at 08:12
  • @NikitaGoncharuk, once verify that server sent you a response as `string` json format or any else? – er-sho Oct 25 '18 at 09:53
  • @NikitaGoncharuk, I think you forgot to view my answer below view it, might be it help you :) – er-sho Nov 01 '18 at 12:45

2 Answers2

3

is there a better variant?

Yes you can parse your json escaped string to JObject.

And then you can access any key/value pair from json with Querying JSON with LINQ

Or you can map your JObject to your custom type by using var result = jObject.ToObject<T>();

class Program
{
    static void Main(string[] args)
    {
        var zohozoho_atliview92 = "{\"Itinerary\":[ {\"Client_Email\":\"garymc\", \"Client_Name\":\"Gary\", \"NT_Number\":\"NT-1237\",\"Number_of_Nights\":7, \"ID\":\"24297940\", \"Itinerary_Name\":\"Icelandnights\", \"Tour_Template_Name\":\"Iceland FireDrive\", \"Departure_Date\":\"2018-07-04\"}]}";

        JObject jObject = JObject.Parse(zohozoho_atliview92);

        Console.WriteLine(jObject);

        Console.ReadLine();
    }
}

Output:

enter image description here

er-sho
  • 9,581
  • 2
  • 13
  • 26
-1

This is not JSON, it's Javascript (wich object declaration is JSON).

Regular expressions are slow, I would advise you to use Substring

var start=inputString.IndexOf("[");
var end=("]");
var json=inputString.Substring(start, end-start);

There might be some of by one errors, test and correct. It would be even faster but weaker to hardcode start.

bokan
  • 3,601
  • 2
  • 23
  • 38