0

I have a json file as following:

JSON File

and I wish to parse them using C#, I tried the Json from Newtonsoft but when I parsed it I got a null and I also tried to paste the Json data as a string in C# but seems like there are many syntax errors in a json data string.

var source = File.ReadAllText("Test/jsonfile.json");
dynamic stuff = JObject.Parse(source);
string name = stuff.hotelID;

1 Answer 1

1

You can use JavaScriptSerializer with its Deserialize method.

var source = File.ReadAllText("Test/jsonfile.json");
var JavaScriptSerializer MySerializer = new JavaScriptSerializer();
var myObj = MySerializer.Deserialize<T>(source);
var htoelId = myObj.searchResults[0].hotelID;

where T is your object.

Also, if you have large files, set

MySerializer = new JavaScriptSerializer { MaxJsonLength = int.MaxValue };
Sign up to request clarification or add additional context in comments.

2 Comments

Yes, thank you but i still dont get how to get the ID or other properties in this case, could you suggest me some more ideas? Thanks @arpad
@bluewonder I've updated my answer. You can map your object as it is. I see that you have a list of search results. So maybe the answer should be myObj.searchResults[0].hotelID . But that is for you to decide.

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.