0

I have a Json in file added to my project embedded resource. I want to read this file which contains a list of certain object and deserialize it.

I have following Json file:

[
    {
        "Status": 21,
        "CustomerId": "e3633ccb-bbea-465d-9ce6-6c37e9c40e2e"
    },
    {
        "Status": 20,
        "CustomerId": "d02e2970-7c28-41b0-89f3-5276a97e12c9"
    }
]

and following model:

public class CustomerStatus 
{
   public int Status { get; set; }
    public string CustomerId { get; set; }
}

as I read the jSon file from resource, it is automatically in form of array of byte and as I convert it to string, it has \r\n and \t in it.
in my code I have following lines do achieve this but it fails:

var string customerdata =   System.Text.Encoding.UTF8.GetString(myResources.CustomerStatus);
var data = JsonConvert.DeserializeObject<List<CustomerStatus>>(customerdata);  

I receive this error: threw an exception of type 'Newtonsoft.Json.JsonReaderException' Message "Unexpected character encountered while parsing value: . Path '', line 0, position 0."

UPDATE:

following line also result the same issue:

var string customerdata =   System.Text.Encoding.UTF8.GetString(myResources.CustomerStatus);
    .Replace("\r\n", " ")
    .Replace("\t", " ");
1
  • 2
    I'll bet you there's a byte-order mark at the beginning of the file and it's ending up in the string. Check if customerdata's first character's character code is 0xFEFF. Commented Oct 1, 2016 at 10:22

3 Answers 3

1

It seems an encoding problem of the file. You can use Notepad++ to convert the file or you can open your JSON file using Notepad and use Save As selecting ANSI as encoding. Then Remove and Add the file again in your resources and switch System.Text.Encoding.UTF8.GetString(myResources.CustomerStatus)

to

System.Text.Encoding.ASCII.GetString(myResources.CustomerStatus)

If you are keen to use UTF8, then use Notepad++ to convert the file instead.

Sign up to request clarification or add additional context in comments.

Comments

1

Try using Net Library methods

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Script.Serialization;

namespace ConsoleApplication16
{
    class Program
    {
        static void Main(string[] args)
        {
            string customerdata = 
                "[" +
                    "{" +
                        "\"Status\": 21," +
                        "\"CustomerId\": \"e3633ccb-bbea-465d-9ce6-6c37e9c40e2e\"" +
                    "}," +
                    "{" +
                        "\"Status\": 20," +
                        "\"CustomerId\": \"d02e2970-7c28-41b0-89f3-5276a97e12c9\"" +
                    "}" +
                "]";

            JavaScriptSerializer ser = new JavaScriptSerializer();
            var r = ser.Deserialize<List<CustomerStatus>>(customerdata);

        }

    }
    public class CustomerStatus
    {
        public int Status { get; set; }
        public string CustomerId { get; set; }
    }
}

1 Comment

I thought putting the file in the resource would be more neat and more readable. do you know why it fails?
0

Try following approach for embedded resources:

using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Reflection;

namespace StackOverflowTests {
  class Program {
    static void Main(string[] args) {
      var assembly = Assembly.GetExecutingAssembly();
      var resource = "StackOverflowTests.StatusCustomer.json";

      var deserializedResource = GetEmbeddedResource<List<CustomerStatus>>(assembly, resource);

      Console.WriteLine(JsonConvert.SerializeObject(deserializedResource));
      Console.ReadKey();
    }

    private static T GetEmbeddedResource<T>(Assembly assembly, string resource) {
      using (Stream manifestStream = assembly.GetManifestResourceStream(resource))
      using (StreamReader reader = new StreamReader(manifestStream)) {
        string result = reader.ReadToEnd();
        return JsonConvert.DeserializeObject<T>(result);
      }
    }

    public class CustomerStatus {
      public int Status { get; set; }
      public string CustomerId { get; set; }
    }
  }
}

If you cant use described approach, post serialized string after

System.Text.Encoding.UTF8.GetString(myResources.CustomerStatus)

call, so i can look what is wrong.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.