Click here to Skip to main content
15,887,812 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more: , +
Hi, everyone! I'm confused, i am new in programming and i need to professionals' help. I have mdf based database, and json data on web server. I must download all data from Web, and deserialize it and add to mdf datatable. Somebody help me.

Json Data:
[ {"id":13422,"user_name":"HaMe","id_number":"888888887052","card_number":"297c2231","breakfasts":"1111100","lunchs":"1111100","dinners":"0000000","start_date":"01/10/2015 00:01","end_date":"25/05/2016 23:59","canteen":"Askhana 1","only_once":false},
{"id":13516,"user_name":"КаBa","id_number":"888888887400","card_number":"293ca641","breakfasts":"1111111","lunchs":"1111111","dinners":"1111111","start_date":"11/10/2015 00:01","end_date":"31/12/2015 23:59","canteen":"Askhana 1","only_once":true},
Posted
Updated 6-Jan-16 16:29pm
v2
Comments
yeleswarapu ramakrishna 6-Jan-16 15:54pm    
first of all your json is not valid post proper json for us to help u out
Temirgali 6-Jan-16 22:30pm    
What about this JSON? What's wrong with it?
yeleswarapu ramakrishna 7-Jan-16 1:23am    
i think your json should look like this
[{
"id": 13422,
"user_name": "HaMe",
"id_number": "888888887052",
"card_number": "297c2231",
"breakfasts": "1111100",
"lunchs": "1111100",
"dinners": "0000000",
"start_date": "01/10/2015 00:01",
"end_date": "25/05/2016 23:59",
"canteen": "Askhana 1",
"only_once": false
}, {
"id": 13516,
"user_name": "КаBa",
"id_number": "888888887400",
"card_number": "293ca641",
"breakfasts": "1111111",
"lunchs": "1111111",
"dinners": "1111111",
"start_date": "11/10/2015 00:01",
"end_date": "31/12/2015 23:59",
"canteen": "Askhana 1",
"only_once": true
}]
yeleswarapu ramakrishna 7-Jan-16 1:27am    
at the end of json the last brace will not be followed with comma and square bracket is also missing i doubt if you posted the full json here u just copy pasted half of it i guess or your json itself is wrong
Temirgali 7-Jan-16 3:07am    
yes, you are right. my json like you typed, I just copied part, I guess it is understandable for developers. but, there problem isn't with json, problem with getting it. How can I map it into datatable. I don't have any idea.

1 solution

C#
using System;
using System.Collections.Generic;
using System.Net;
using System.Web.Script.Serialization;
public partial class Practice : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        List<YourClassName> objYourClass = downloadJsonData("http:/185.98.5.184:9000/users/dealed/Test");//use this class properties where ever necessary
    }
    private static List<YourClassName> downloadJsonData(string url) 
    {
        var w = new WebClient();
        var jData= w.DownloadString(url);
        return !string.IsNullOrEmpty(jData) ? new JavaScriptSerializer().Deserialize<List<YourClassName>>(jData) : null ;
    }
}
public class YourClassName
{
    public int id { get; set; }
    public string user_name { get; set; }
    public string card_number { get; set; }
    public string breakfasts { get; set; }
    public string lunchs { get; set; }
    public string dinners { get; set; }
    public string start_date { get; set; }
    public string end_date { get; set; }
    public string canteen { get; set; }
    public bool only_once { get; set; }
    //public List<object> gtransactions { get; set; } //in this case u don't need to declare Gtransactions class
    //public List<Gtransactions> gtransactions { get; set; } //in this case avoids boxing unboxing 
    public Gtransactions[] gtransactions { get; set; } //in this case avoids boxing unboxing as well 
    public string id_number { get; set; }
}
public class Gtransactions
{
    public string gdeal_id { get; set; }
    public string meal_time { get; set; }
    public bool jana { get; set; } 
}
 
Share this answer
 
v2
Comments
yeleswarapu ramakrishna 7-Jan-16 9:37am    
use this instead of the above because it comments out entire line so i wrote that http://185.98.5.184:9000/users/dealed/Test
Temirgali 7-Jan-16 22:07pm    
Thank you very much! It really helps to me.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900