8

I have a Json String that I get from a web service; it has a list of collections, each collection represents an object, for example:

  [ // Root List
    [ // First Collection : Team Object
      {
        "id": 1,
        "team_name": "Equipe Saidi",
        "is_active": true,
        "last_localisation_date": "2015-05-06T13:33:15+02:00"
      },
      {
        "id": 3,
        "team_name": "Equipe Kamal",
        "is_active": true,
        "last_localisation_date": "2015-05-06T09:22:15+02:00"
      }
     ],
     [// Second Collection : user Object
      {
        "id": 1,
        "login": "khalil",
        "mobile_password": "####",
        "first_name": "Abdelali",
        "last_name": "KHALIL",
        "email": "[email protected]",
        "role": "DR",
        "is_active": true,
        "charge": false
      },
      {
        "id": 2,
        "login": "ilhami",
        "mobile_password": "####",
        "first_name": "Abdellah",
        "last_name": "ILHAMI",
        "email": "[email protected]",
        "role": "DR",
        "is_active": true,
        "charge": false
      }
    ]
  ]

My actual code (not working of course ):

 public async Task TeamsAndMobileUsers()
    {
        string data = "";
        IList<User> MobileUsersList = new List<User>();
        IList<Team>  TeamsList  = new List<Team>();
        try
        {
            data = await GetResponse(PATH + TEAMS_USERS_URL);
            TeamsList = JsonConvert.DeserializeObject<List<Team>>(data);   
           MobileUsersList = JsonConvert.DeserializeObject<List<User>>(data); 

            // Inserting
            await SetAchievedActions(TeamsList);

        }
        catch (Exception e) { 
            _errors.Add(e.Message); 
        }
    }

I use Json.net and C#. I can't find a solution, I've read that I should use JsonReader and set its SupportMultipleContent property to true but I don't know how to implement that solution.

3
  • 1
    Something is wrong with your JSON. It is invalid and incorrect in terms of logic. You have an array of two objects of different types. How do you want it to work together? Actually, they should be two objects. Are you able to edit JSON structure or receive it in another format? Commented Jun 2, 2015 at 10:49
  • 1
    possible duplicate. Validate, generate class then JsonConvert.DeserializeObject<RootObject>(string json); Commented Jun 2, 2015 at 10:51
  • @Yeldar Kurmangaliyev No , i can't edit Json Structure :( Commented Jun 2, 2015 at 11:00

3 Answers 3

8

As @YeldarKurmangaliyev already said, your json has two different objects, I think you can do something like this:

var j = JArray.Parse(data);
TeamsList = JsonConvert.DeserializeObject<List<Team>>(j[1].ToString());
MobileUsersList = JsonConvert.DeserializeObject<List<User>>(j[2].ToString());
Sign up to request clarification or add additional context in comments.

Comments

1

have you tried http://json2csharp.com/ to generate contract classes for that json? also, first and last parenthesis gives a not valid JSON

1 Comment

yeah you are right afer deleting the parenthesis, it passes JsonLint validation, but fails at Json2csharp generation .
-4
You need to create 4 classes
1st class TeamObject : Variable(id,team_name,is_active,last_localisation_date)
2nd class UserObject : Variable (id, login,mobile_password,first_name, last_name ,                        email, role,is_active,charge)
3rd class RootList: Variable ( arraylist<TeamObject> obj, arraylist<UserObject > obj2)
4th class RootClass : Variable(arraylist<RootList> obj)
Gson gson=new Gson();
RootClass dtomodel = gson.fromJson(data , RootClass .class);


This parsing done using Gson Library

1 Comment

Why Should I create 4 classes , knowing that i have two objects only user & team !?. Gson is a java Library not c#.

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.