-2

I have a json String(array) like this:

[{"user":{"name":"abc","gender":"Male","birthday":"1987-8-8"}},    
{"user":{"name":"xyz","gender":"Female","birthday":"1987-7-7"}}]

and want to parse it to json object using .net4.0 framework only, and i cannot use DataContractJsonSerializer as it requires class and i am receiving dynamic data over web services within 1 minute which keep changing and it is in Name-value format,i tried using JavaScriptSerializer but i am unable to add system.web.extension in my vs2010 project .net4.0 supported framework,and i don't want to use any third party library,actually i am New-bie in wpf,so please it would be great help,thanks in advance!

alexander.polomodov
  • 5,396
  • 14
  • 39
  • 46
sailee
  • 1
  • 1

1 Answers1

1

Well there are two main issues I can see here, and one additional.

1) If you do not have particular class that you could deserialize JSON to, then you have to rely on some "dictionary-like" structure (e.g. dynamic or JToken)to be able to access all fields. However data you presented seems to be structured, so maybe consider creating POCO to get advantage of strongly-typed structure. Both can be easily achieved using ready-to-use libraries.

2) You say you don't want to use any third party library, but actually there is nothing wrong with it. Actually you should be doing so to avoid reinventing the wheel as Tewr mentioned. It's perfectly fine to use in fact industry-standard library such as Newtonsoft Json so you can avoid tons of bugs, unnecessary work and future troubles. If your point is to learn by writing JSON (de)serializer it's perfectly fine, but I'd recommend against using it in production code.

Side note: you mentioned you receive data over web-service, and it seems you receive simply JSON array (as top-level object). It's considered as security hole. More information may be found here:

EDIT 2017-11-05:

Ok, so you should create classes representing response from your web service (you can use feature of VS called Edit > Paste Special > Paste JSON As Classes):

public class Response
{
    public User[] users { get; set; }
}

public class User
{
    public string Name { get; set; }
    public string gender { get; set; }
    public string birthday { get; set; }
}

Now using Nuget install package Newtonsoft.Json and using following code you'll deserialize JSON response to .NET classes:

string responseText = "";//Get it from web service
var response = JsonConvert.DeserializeObject<Response>(responseText);

Hope this help!

Yakuza
  • 3,237
  • 2
  • 21
  • 18
  • is their any method through which i can deserialize json string to object?or is it possible to do so? – sailee Nov 04 '17 at 15:59
  • Well, actually deserializing to both `dynamic` and `JToken` can be considered as deserializing to `object`. Could you elaborate more on what you mean by "deserilze to object"? What is a goal you want to achieve? – Yakuza Nov 04 '17 at 23:03
  • {"users":[{ "Name":"xyz". "gender":"male", "birthday":"1987-8-8" }, { "Name":"xyz". "gender":"male", "birthday":"1987-8-8" } ]} this is something i want to achieve as in object form,this is output of above string which i mentioned above,i was asking in this context for a particular method,it would be of great help thanks in advance,i searched a lot for this requirement of mine but was unsuccessful,thanks! – sailee Nov 05 '17 at 06:21
  • yes i got the answer where i can use NewtonsoftJson ultimately,but thanks for the solution. – sailee Aug 08 '18 at 13:46