-1
    {
    "players": [
    {
    "wins": 8,
    "losses": 1,
    "score": 264.5,
    "pro": false,
    "name": "albert"
    },
    {
    "wins": 7,
    "losses": 1,
    "score": 214.5,
    "pro": false,
    "name": "mike"
    }

I want to parse it and I have tried as below:

    Dictionary<string,object> data=MiniJSON.Json.Deserialize(json) as Dictionary<string,object>;
    List<object> list=(List<object>)(data["Person"]);
    //data["players"] as List<object>
    foreach(var item in data)
    {
     // data as Dictionary<string,object>
    Dictionary<string,object> dict=(Dictionary<string,object>)(data);
    }

I Tried But I don't know whether It is coorect or not and I Have not able to proceed further please spare a minute to help me

dbc
  • 104,963
  • 20
  • 228
  • 340
Rock
  • 27
  • 5
  • 1
    You're using unity, correct? Then maybe see [Serialize and Deserialize Json and Json Array in Unity](https://stackoverflow.com/q/36239705/3744182). [Parse Nested JSON with MiniJSON (Unity3D)](https://stackoverflow.com/q/22739791) might help also. But you wrote, *I Tried But I don't know whether It is coorect or not and I Have not able to proceed further* -- then what problem did you encounter? Was an exception thrown? – dbc Jan 23 '20 at 17:38
  • S i am using unity but the above article i already viewed i dint get any idea from it – Rock Jan 24 '20 at 05:12

1 Answers1

0

1) Your Json is not valid: you can use this site to validate json. You should add ']' and '}'. So you json should look like:

{
    "players": 
    [
        {
            "wins": 8,
            "losses": 1,
            "score": 264.5,
            "pro": false,
            "name": "albert"
        },
        {
            "wins": 7,
            "losses": 1,
            "score": 214.5,
            "pro": false,
            "name": "mike"
        }
    ]
}

2) You can use Unity JsonUtility. Create class Player

[Serializable]
public class Player
{
    int wins;
    int losses;
    int score;
    bool pro;
    string name;
}

Then create player container

public class PlayerContainer
{
    List<Player> players = new List<Player>();
}

Now you can use JsonUtility to serialize/deserialize your data

public class PlayersController
{
    private void Start()
    {
        string json = "your json";
        var players = JsonUtility.FromJson<PlayerContainer>(json);
    }
}
Nikolay Gonza
  • 603
  • 5
  • 18
  • Thanks a lot but I Have to use Minijson only if u can tell me that it is more useful Anyway Thanks for sparing a moment to Help Me – Rock Jan 24 '20 at 14:25
  • just use this code https://stackoverflow.com/questions/22739791/parse-nested-json-with-minijson-unity3d and fix your json text – Nikolay Gonza Jan 24 '20 at 14:28
  • Sir It Would be helpful if u answer as u answered like above clearly please – Rock Jan 24 '20 at 14:55
  • string json = "your json"; var dict = Json.Deserialize(json as Dictionary; List players= dict["players"] as List; – Nikolay Gonza Jan 24 '20 at 14:59
  • Is that enough sir for deserialization and how to add it to list? – Rock Jan 24 '20 at 15:13