Newboy11
Newboy11

Reputation: 3136

Unexpected character encountered while parsing value on JSON net

I'm new to JSON in C#. I'm planning to use newtonsoft json for saving data in my game. But encountered error "Unexpected character encountered while parsing value: C. Path '', line 0, position 0.". I don't know whats causing this. Below is my json data, models and code. What I'm trying to achieve is update a json file then save it

JSON Data

{"NextLevel":1}

Model

public class GameInfo 
{
    public int NextLevel { get; set; }
}

Code

string path = Application.persistentDataPath + "/testtesJSON";

var gmeInfo = JsonConvert.DeserializeObject<GameInfo>(path);

I'm getting error on DeserializeObject part

Upvotes: 0

Views: 1204

Answers (1)

Vivek Nuna
Vivek Nuna

Reputation: 1

As I understood correctly, you want to read a file from a path and then want to deserialize its content to a class object. I am assuming here testtesJSON is your file name.

You should do like this.

string path = Path.Combine(Application.persistentDataPath, "testtesJSON");
string content = File.ReadAllText(path);
var gmeInfo = JsonConvert.DeserializeObject<GameInfo>(content);

Based on your latest update to this question and discussion in the comment.

Its very simple, it should be like this. you can ignore the compilation errors as I have not complied with the code.

gmeInfo.NextLevel = 2;
var newContent = JsonConvert.SerializeObject(gmeInfo);
File.WriteAllText(yourPath, newContent);

Upvotes: 2

Related Questions