I need to get some data from json file i've got it to the string named dataAsJson, on debugging it looks like that:
{\r\n\t\"question\":\r\n\t[\r\n\t\t{\r\n\t\t\"text\":
\"głośniki\",\r\n\t\t\"correct\": \"speaker\",\r\n\t\t\"answer1\":
\"speaker1\",\r\n\t\t\"answer2\": \"speaker2\",\r\n\t\t\"answer3\":
\"speaker3\"\r\n\t\t},\r\n\t\t{\r\n\t\t\"text\":
\"pustynia\",\r\n\t\t\"correct\": \"desert\",\r\n\t\t\"answer1\":
\"desert1\",\r\n\t\t\"answer2\": \"desert2\",\r\n\t\t\"answer3\":
\"deser3\"\r\n\t\t},\r\n\t\t{\r\n\t\t\"text\":
\"rycerz\",\r\n\t\t\"correct\": \"knight\",\r\n\t\t\"answer1\":
\"knight1\",\r\n\t\t\"answer2\": \"knight2\",\r\n\t\t\"answer3\":
\"knight3\"\r\n\t\t}\r\n\t]\r\n}
And then i use JsonUtility.FromJson(dataAsjJson)
This is my objects classes:
public class Question
{
public string text { get; set; }
public string correct { get; set; }
public string answer1 { get; set; }
public string answer2 { get; set; }
public string answer3 { get; set; }
}
public class RootObject
{
public List<Question> questions { get; set; }
}
As you can see the json body is only the "question" with array.
On Debug.log(dataAsJson) it looks normally:
{
"question":
[
{
"text": "głośniki",
"correct": "speaker",
"answer1": "speaker1",
"answer2": "speaker2",
"answer3": "speaker3"
},
{
"text": "pustynia",
"correct": "desert",
"answer1": "desert1",
"answer2": "desert2",
"answer3": "deser3"
},
{
"text": "rycerz",
"correct": "knight",
"answer1": "knight1",
"answer2": "knight2",
"answer3": "knight3"
}
]
}
I need to convert it succesfully to C# object.
As mentioned by Amy in the comments, your C# class structure doesn't match your JSON, I use http://json2csharp.com/ because it's accurate and quick. Make sure you put [Serializable] above each class so we can transform data structures or object states into a format that Unity can store and reconstruct later read more here. Lastly, I would avoid getters and setters in this structure, it will cause problems with serializing & is recommended by unity to use fields instead.
[Serializable]
public class Question
{
public string text;
public string correct;
public string answer1;
public string answer2;
public string answer3;
}
[Serializable]
public class RootObject
{
public List<Question> question;
}
Related
I have an API that returns a JSON string and I want to parse that JSON string into an object. I tried creating the object but with no luck. Below is the sample JSON string that I want to get the value from. Any idea as to what the class looks like in order to parse that JSON object into an object? My main concern is to get the code which is "platinum" under currentCard.
{
"status" : {
"currentCard" : {
"code" : "platinum"
},
"status" : {
"index" : 0,
"value" : "This is a sample text."
}
}
}
You need to use a website such as https://json2csharp.com/ or use the inbuilt tools in VS Studio (Edit->Paste Special->Paste JSON as classes) which will automatically create classes from JSON.
The above mentioned website suggests the following:
public class CurrentCard
{
public string code { get; set; }
}
public class Status2
{
public int index { get; set; }
public string value { get; set; }
public CurrentCard currentCard { get; set; }
public Status status { get; set; }
}
public class Root
{
public Status status { get; set; }
}
Which you can then deserialise like this:
Root myDeserializedClass = JsonConvert.DeserializeObject<Root>(myJsonResponse);
I'm creating a little system to let me see who is in my chat on Twitch using their JSON API. However, while I successfully got the information, I can't figure out how to parse it correctly.
This is the string that is being produced:
{
"_links": {},
"chatter_count": 1,
"chatters": {
"moderators": [
"teonnyn"
],
"staff": [],
"admins": [],
"global_mods": [],
"viewers": []
}
}
This is the object I created to deserialize it to, but I have no idea for sure if it's exactly correct:
public class users
{
public string[] links;
public int chatter_count;
public string[] moderators;
public string[] staff;
public string[] admins;
public string[] global_mods;
public string[] viewers;
}
I'm using Newtonsoft.JSON to parse it - which would be the correct way to push the string to the "users" object?
No, the C# class you have doesn't really correlate correctly to the JSON:
Your links member doesn't match the name _links in JSON.
_links is defined as an array, but should be an object - it's {} in JSON, not [].
Likewise chatters, which should be a custom class as well.
Starting with Visual Studio 2013 Update 2, you can generate a C# class from a JSON sample. This is what it generated for your JSON:
public class Rootobject
{
public _Links _links { get; set; }
public int chatter_count { get; set; }
public Chatters chatters { get; set; }
}
public class _Links
{
}
public class Chatters
{
public string[] moderators { get; set; }
public object[] staff { get; set; }
public object[] admins { get; set; }
public object[] global_mods { get; set; }
public object[] viewers { get; set; }
}
As you can see, it maps moderators properly to a string[] but gets a bit confused and uses object[] for the rest, because the snippet contains to data for it to base the type on.
If you can get a JSON sample with more data - ideally, with every field being present and having representative data - you'll get the best mapping.
Also, you should change Rootobject to your own class name, of course. User or TwitchUser should do it.
Once you have a class that corresponds correctly to your JSON, using JSON.NET to parse it is very simple:
Rootobject yourData = JsonConvert.DeserializeObject<Rootobject>(inputJsonString);
And you're done.
I'm facing some problems with the read of a JSON file, which is this one:
{
"giocatori": [
{
"Giocatore": "124",
"Cognome": "DE SANCTIS",
"Ruolo": "P",
"Squadra": "ROM"
},
{
"Giocatore": "140",
"Cognome": "MIRANTE",
"Ruolo": "P",
"Squadra": "PAR"
},
{
"Giocatore": "156",
"Cognome": "SKORUPSKI",
"Ruolo": "P",
"Squadra": "ROM"
}
],
"success": 1
}
What I want to get from this PHP is an List, where the Player's class with this attributes;
public string Giocatore;
public string Cognome;
public string Ruolo;
public string Squadra;
I don't know why, but I face some problems with the Microsoft.Json library, in particular with Json.DeserializeObject> method, which is not able to read that web page. Can you provide some hint how to obtain a List in C# of Player ? Thank you so much for your support !
Go to http://json2csharp.com/, post your JSON there, and get the following classes:
public class Giocatori
{
public string Giocatore { get; set; }
public string Cognome { get; set; }
public string Ruolo { get; set; }
public string Squadra { get; set; }
}
public class RootObject
{
public List<Giocatori> giocatori { get; set; }
public int success { get; set; }
}
To deserialize your JSON string with JavaScriptSerializer, do:
var root = new System.Web.Script.Serialization.JavaScriptSerializer().Deserialize<RootObject>(jsonString);
var list = root.giocatori;
To deserialize your JSON string with Json.NET, a widely used, free, open source JSON serializer, download and install it according to the instructions on the home page and do:
var root = Newtonsoft.Json.JsonConvert.DeserializeObject<RootObject>(jsonString);
var list = root.giocatori;
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have JSON in this format:
{
"user": "123#abc",
"filter": [
{
"filterName": "Filter1",
"condition": "1",
"values": [
"123"
]
},
{
"filterName": "Filter2",
"condition": "2",
"values": [
"ABC",
"XYZ"
]
}
],
"onlyMerge": "true",
"mergeBy": [
]
}
And I am using these classes
public class Outermost{
public string user;
public Root filter ;
public string onlyMerge;
public string mergeby;
}
public class values {
public string value { get; set; }
}
public class Filters {
public string filtername {get; set; }
public string condition {get; set;}
public values values { get; set; }
}
public class Root {
public List<Filters> Filters { get; set; }
}
JSONConvert.Deserialize(Outermost)
I have to deserialize the structure
Paste your JSON into http://json2csharp.com/ and you will see that your C# classes don't match the JSON.
Update 2:
Visual Studio 2013 has a built-in Json to C# Class converter tool! :)
Update:
Just a note about the great tool http://json2csharp.com/ : When working with a object that has properties of complex types, You may want to check about the classes it creates, because sometimes it'll created unnecessarily/undesirable classes. Example:
Json
var objJson = {
"id_product": 19,
"description": "Laptop",
"_links": {
"buy": {
"href": "/Product/Buy/19",
"title": "Buy It Now!"
},
"more_details": {
"href": "/Product/Details/19",
"title": "More Details..."
}
}
};
Generated Class/Classes:
public class Buy
{
public string href { get; set; }
public string title { get; set; }
}
public class MoreDetails
{
public string href { get; set; }
public string title { get; set; }
}
public class Links
{
public Buy buy { get; set; }
public MoreDetails more_details { get; set; }
}
public class RootObject
{
public int id_product { get; set; }
public string description { get; set; }
public Links _links { get; set; }
}
As You can see, the two classes Buy and MoreDetails have exactly the same structure and purpose, so You may want to replace it with a more generic class instead of using repeatedly classes (there are scenarios where this redundant structure is more appropriate). In a very similar scenario, I've created a class named Link.
Original Answer:
You don't said enough to be sure what's your problem. Try in the future specify better what are your difficulties and needs.
But, I guess your problem is some exception being throw or some properties not being bind...
If you pay attention, in your JSON example object, filter is directly a collection, and not a property that has a collection inside. Thus, just change
public Root filter; to public List<Filters> filter { get; set; }.
Also, mergeBy and values are collections, and not simple strings. You could use http://json2csharp.com/ to generate automatically the correspondent C# class of your JSON object, and check what properties are not matching... (Or, substitute your whole class, that is what I would recommend...)
Hi I am having trouble deserializing some JSON into a c#.net class using newtonsoft desrializeobject
Example JSON
[
[
{
"id": "1",
"Colour": "Red"
},
{
"cid": "1",
"Shape": "Square"
},
{
"cid": "2",
"Shape": "Circle"
}
]
]
I want this to appear in my C#.net class like this excuse the typos and syntax but you get the general idea.
public class object {
public int id;
public string colour;
public Shape[] shapes;
}
public class Shape {
public int cid;
public string shapename;
}
how can I achieve this?
See my answer over here:
json serialize list in list
You should create a RootObject first. Which represents the first List.
The RootObject contains a List with your Shape class
You could also specify the [JsonProperty] annotations to make sure the correct Json properties are mapped to your Shape object.
Basically, that's not what the class is serializing into. If you paste that JSON as a class in Visual Studio, and you get this:
public class Rootobject
{
public Class1[][] Property1 { get; set; }
}
public class Class1
{
public string id { get; set; }
public string Colour { get; set; }
public string cid { get; set; }
public string Shape { get; set; }
}
So the issue is the JSON. If that's not your JSON and you can't do anything to fix the formatting of it, I'd serialize it into a dynamic object first and then do the necessary transformations.