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.

7
  • 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... Commented May 25, 2019 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, 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. Commented May 25, 2019 at 17:34
  • @dbc I persume you mean to return the List instead of encoding it ?. Commented May 25, 2019 at 17:36
  • Also related: How do I return clean JSON from a WCF Service? and Returning raw json (string) in wcf. 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 shows another way. Commented May 25, 2019 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. Commented May 25, 2019 at 17:38

1 Answer 1

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);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.