0

I am using netownsoft json.net to serlize an object but its adding string at the start I dont under stand why its doing this.

Newtonsoft.Json.JsonReaderException: Unexpected character encountered while parsing value: <. Path '', line 0, position 0.

public async Task<T> GetDataFromSageService<T>(string url, params string[] args)
where T : class
{
    var uri = new Uri(string.Format(url, args));

    var response = await _client.GetAsync(uri);

        if (response.IsSuccessStatusCode)
        {
            var content = await response.Content.ReadAsStringAsync();
            return Newtonsoft.Json.JsonConvert.DeserializeObject<T>(content);
        }

        return default(T);
}

I am using the following to encode my end point which is hosted in a wcf service.

public string GetWarehouses()
{
        DataSet ds = new SqlDa().GetWarehouses();
        ds.Tables[0].TableName = "Warehouses";
        return JsonConvert.SerializeObject(ds, Formatting.Indented);
}

But the string i am getting back is as such

 <string xmlns="http://schemas.microsoft.com/2003/10/Serialization/">
{
 "Warehouses": [
 {
 "WarehouseID": 13016489,
 "Name": "B",
 "Description": "Belfast "
 },
 {
 "WarehouseID": 13016647,
 "Name": "B",
 "Description": "B"
 },
 {
 "WarehouseID": 13815467,
 "Name": "Direct Delivery",
 "Description": ""
 },
 {
 "WarehouseID": 1008,
 "Name": "PW",
 "Description": "Postal Way"
 },
 {
 "WarehouseID": 13016234,
 "Name": "H",
 "Description": "Hospital"
 },
 {
 "WarehouseID": 13016238,
 "Name": "MPC",
 "Description": "Clinic"
 },
 {
 "WarehouseID": 13029366,
 "Name": "O",
 "Description": "Outpatient"
 },
 {
 "WarehouseID": 13815466,
 "Name": "Returns",
 "Description": ""
 }
 ]
 }
</string>

As You can see its enclosed it as a string for some reason and don't understand as to why. Is their a way with the data set to make sure that it gets converted into proper json.

dbc
  • 104,963
  • 20
  • 228
  • 340
c-sharp-and-swiftui-devni
  • 3,743
  • 4
  • 39
  • 100
  • What you see there is an XML tag, and it is rather unlikely (read: zero chance) to have been added by `JsonConvert.SerializeObject`. I suggest you look closely at what your code does with the string returned by the GetWarehouses() method. (Use the debugger to inspect the true string value returned by this method. You will see that it will not contain that XML tag.). Somewhere along the way that string will get modified, with that XML tag being added/inserted surrounding the original json string. Where? Don't ask me, i don't know your code... –  May 25 '19 at 17:31
  • That's an XML string wrapped around a JSON string. This looks to be a variation on [JSON.NET Parser *seems* to be double serializing my objects](https://stackoverflow.com/q/25559179/3744182), only in this case you're serializing to a JSON string then your server is serializing that string to XML rather than to JSON also. Same solution though, which is to return some DTO's directly from the service. – dbc May 25 '19 at 17:34
  • @dbc I persume you mean to return the List instead of encoding it ?. – c-sharp-and-swiftui-devni May 25 '19 at 17:36
  • Also related: [How do I return clean JSON from a WCF Service?](https://stackoverflow.com/q/2086666) and [Returning raw json (string) in wcf](https://stackoverflow.com/q/3078397). If you really need to return pre-serialized JSON via WCF, the latter shows one example. [How to set Json.Net as the default serializer for WCF REST service](https://stackoverflow.com/a/3131413/3744182) shows another way. – dbc May 25 '19 at 17:37
  • @rogue39nin - yes exactly. WCF is designed to automate the sending of data contract objects over the wire, not send pre-serialized literals. – dbc May 25 '19 at 17:38
  • Also [How can I return json from my WCF rest service (.NET 4), using Json.Net, without it being a string, wrapped in quotes?](https://stackoverflow.com/q/3026934) and [WCF returns xml instead of JSON](https://stackoverflow.com/q/23806268) – dbc May 25 '19 at 17:44
  • ... I think your question can be made a duplicate of [How do I return clean JSON from a WCF Service?](https://stackoverflow.com/q/2086666/3744182) and [Enable WCF Service to use with JSON](https://stackoverflow.com/q/16144873/3744182). Agree? – dbc May 25 '19 at 17:54

1 Answers1

0

If you don't want to modify your server code, you could use regex to extract legal json string from your response.

       string content ="your context with xml"
        Regex regex = new Regex("<string\\s*xmlns=\".*\">([\\s\\S]*)</string>");
       Match match =  regex.Match(content);
        Response.Write(match.Groups[1].Value);
        Newtonsoft.Json.JsonConvert.DeserializeObject(match.Groups[1].Value);
Ackelry Xu
  • 756
  • 3
  • 6