Google Parsing Json array objects in windowsphone c# | SubramanyamRaju Xamarin & Windows App Dev Tutorials

Saturday, 30 November 2013

Parsing Json array objects in windowsphone c#

Introduction

HTTP-based Web services frequently use JavaScript Object Notation (JSON) messages to return data back to the client. JSON objects can easily be instantiated in JavaScript, without parsing logic, which makes it a convenient format for messages in Web applications.You may get solve this problem using JObject class or any third party,its is not a best solution to use third party.So i come up with my best option is "parsing json using DataContractJsonSerializer".How it is most important concept in wpf,so now i tried this in wp7.


Building the Sample

Before going to start parsing json.You must be add reference to both "system.runtime.serialization" and "system.servicemodel.web".And in this sample i am using http://json2csharp.com/ for simply building a C# class from JSON formated string.And its very important is to make class as similar to Json objects.Other wise we will never parse date properly.

Source File at :  Parsin JsonArray in windows phone

Description
This is time I am going to cover DataContract JSON Serialization using DataContractJsonSerializer.  DataContract JSON Serialization is mostly used by Windows Communication Foundation (WCF) services, but I will try to apply this approach to Windows Phone 7 environment.   

C#
using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Net; 
using System.Windows; 
using System.Windows.Controls; 
using System.Windows.Documents; 
using System.Windows.Input; 
using System.Windows.Media; 
using System.Windows.Media.Animation; 
using System.Windows.Shapes; 
using Microsoft.Phone.Controls; 
using System.Runtime.Serialization.Json; 
using System.IO; 
using System.Text; 
using System.Runtime.Serialization; 
 
namespace Json_Parsing 
{ 
    public partial class MainPage : PhoneApplicationPage 
    { 
        // Constructor 
        public MainPage() 
        { 
            InitializeComponent(); 
 
            string jsonString = @"{ 
    'product': [ 
        { 
            'CATEGORYNAME': [ 
                'AshokLeyland', 
                'HindustanMotorsLtd', 
                'MahindraLtd', 
                'TataMotors', 
                'SwarajMazda' 
            ], 
            'CATEGORYID': [ 
                '1', 
                '2', 
                '3', 
                '4', 
                '6' 
            ], 
            'SUBCATEGORYNAME': [ 
                'MultiaxleVehicles', 
                'HippoHaulage', 
                'HippoTipper', 
                'Cargo', 
                'Pick-up' 
            ], 
            'SUBCATEGORYID': [ 
                '1', 
                '2', 
                '3', 
                '4', 
                '5' 
            ], 
            'TYPENAME': [ 
                'Haulage', 
                'Tippers', 
                'RigidTrucks', 
                'Cabs', 
                'DeliveryVan' 
            ], 
            'TYPEID': [ 
                '1', 
                '2', 
                '3', 
                '4', 
                '5' 
            ] 
        } 
    ], 
    'success'1, 
    'message''Thetruckdetailsgettingsuccessfully' 
} 
"; 
            RootObject TotalList = new RootObject(); 
            RootObject childlistonly = new RootObject(); 
            Product prdt=new Product(); 
            MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(jsonString)); 
            DataContractJsonSerializer ser = new DataContractJsonSerializer(TotalList.GetType()); 
            TotalList = ser.ReadObject(ms) as RootObject; 
            string category = ""; 
            string typename = ""; 
            int i = 0; 
            int k = 0; 
            foreach (var d in TotalList.product) 
            { 
                foreach (var t in d.CATEGORYNAME) 
                { 
                    category = category + "" + t.ToString() + "\n" + "\n" + "\n"; 
                    i++; 
                } 
                
                foreach (var t in d.TYPENAME) 
                { 
                    typename = typename + "" + t.ToString() + "\n" + "\n" + "\n"; 
                    k++; 
                } 
                foreach (var t in d.SUBCATEGORYNAME) 
                { 
                   // teststring = teststring + "" + t.ToString() + "\n" + "\n" + "\n"; 
                    i++; 
                } 
            } 
            MessageBox.Show("Your CATEGORYNAMES are:\n\n" + category); 
            MessageBox.Show("Your TYPENAMES are:\n\n" + typename); 
            ms.Close(); 
            
             
        } 
    } 
    public class Product 
    { 
        public List<string> CATEGORYNAME { getset; } 
        public List<string> CATEGORYID { getset; } 
        public List<string> SUBCATEGORYNAME { getset; } 
        public List<string> SUBCATEGORYID { getset; } 
        public List<string> TYPENAME { getset; } 
        public List<string> TYPEID { getset; } 
    } 
 
    public class RootObject 
    { 
        public List<Product> product { getset; } 
        public int success { getset; } 
        public string message { getset; } 
    } 
}

Windows Phone tutorials for beginners key points

This section is included for only windows phone beginners.However this article can covers following questions.Off course these key words are more helpful for getting more best results about this topic in search engines like google/bing/yahoo.. 

1. How to parse the Json data or json string in windows phone 8

2.Json Parsing in WindowsPhone using DataContractJsonSerializer

3.Windows Phone JSON Parsing Example in c#

4. Parsing Complicated JSON object in windows phone 8 c#

Have a nice day by 

3 comments:

  1. can u suggest me how i can display data from json on hubtile control in windows phone 8 application??

    ReplyDelete
  2. This article will help you Mr.KINJAN BHAVSAR

    http://developer.nokia.com/community/wiki/How_to_handle_JSON_strings_with_Windows_Phone

    ReplyDelete
  3. i have to make a log in page and using json. send request to server, get response. can u plz pelp me.

    ReplyDelete

Search Engine Submission - AddMe