Click here to Skip to main content
15,891,253 members
Articles / Programming Languages / Javascript

JSON Object Code Example

Rate me:
Please Sign up or sign in to vote.
0.00/5 (No votes)
13 Jun 2011CPOL 15.9K   7   1
An example to demonstrate the power and accessibility JSON offers to developers.

The first example is to demonstrate the power and the accessibility JSON offers to developers. This is a JavaScript code designed to make things easier for the -new to JSON- developer:

JavaScript
function User(fName, lName, age, friends)
{
    var UserObject = {
        FirstName: fName,
        LastName: lName,
        Age: age,
        Friends: [],
        IsUnderAge: function()
        {
            return this.Age > 21 ? true : false;
        }
    };

    var FriendsCounter = 0;
    for (var frnd in friends)
    {
        UserObject.Friends[FriendsCounter] = friends[frnd];
        FriendsCounter++;
    }

    return UserObject;
}

This is the constructor. Same as in every OOP language, each object needs to have constructors.

Now, let's say I let a user named Igor fill an information form containing the following fields:

  1. First Name
  2. Last Name
  3. Age
  4. Friends - each friend will have the same fields.

On the "Submit" button, I have a call for a "CreateUser" function which will take the needed fields (using jQuery - but that's for another post) and send to the User constructor.

JavaScript
function createUser()
{
    var IgorsFriends = [];
    IgorsFriends[0] = new User("Uri", "GamingTech", 29, []);
    IgorsFriends[1] = new User("Sergey", "GamingTech", 35, [
        new User("Noam", "GamingTech", 27, [])
    ]);
    
    var Igor = new User("Igor", "GamingTech", 33, IgorsFriends);
}

The outcome of this function will be a variable named Igor which will have the following attributes:

  • First Name
  • Last Name
  • Age

The following methods:

  • IsUnderAge

And the following arrays:

  • Friends - which will contain a list of friends as User objects.

Good luck!

License

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



Comments and Discussions

 
Questionamit Pin
pradeep rawat20-Jun-11 22:19
pradeep rawat20-Jun-11 22:19 
using System;
using System.Web.Services.Protocols;
using System.IO;
using System.Text;
using System.Xml;
using KYCBL;
using KYCDAL;


public class WebServcSoapExt : SoapExtension
{
private Stream inwardStream;
private Stream outwardStream;
public override Stream ChainStream(Stream stream)
{
outwardStream = stream;
inwardStream = new MemoryStream();
return inwardStream;
}

public override object GetInitializer(Type serviceType)
{
return null;
}

public override object GetInitializer(LogicalMethodInfo methodInfo, SoapExtensionAttribute attribute)
{
return null;
}

public override void Initialize(object initializer)
{
return;
}

public override void ProcessMessage(System.Web.Services.Protocols.SoapMessage message)
{
string soapMsg1;
StreamReader readStr;
StreamWriter writeStr;
switch (message.Stage)
{
case SoapMessageStage.BeforeDeserialize:
readStr = new StreamReader(outwardStream);
writeStr = new StreamWriter(inwardStream);
soapMsg1 = readStr.ReadToEnd();
DataAccessLayer.ExecuteNonQuery(KYCDAL.DataAccessLayer.ProcedureNames.PRC_SAVE_CACHE_LOG.ToString(), "cacheName_", "KYC", "applicationName_", "Bridger Output: " + soapMsg1, "True", "accessKey_");

//soapMsg1 = "<?xml version=\"1.0\" encoding=\"utf-8\"?><soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"><soap:Body><SearchResponse xmlns=\"https://bridgerinsight.lexisnexis.com/webservices/6.0\"><SearchResult><Error><Message>Object already exists.</Message><Code>0</Code></Error></SearchResult></SearchResponse></soap:Body></soap:Envelope>";
//soapMsg1 = "<?xml version=\"1.0\" encoding=\"utf-8\"?><soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"><soap:Body><SearchResponse xmlns=\"https://bridgerinsight.lexisnexis.com/webservices/6.0\"><SearchResult><ClientReference><Reference>Mar21</Reference></ClientReference><RecordList><Record><Status /><Record>1</Record><RequestIndex>0</RequestIndex><ResultID>-1</ResultID><RecordDetailInfo><EntityType>Business</EntityType><WireType>None</WireType><SearchDate>2011-04-11T08:05:36Z</SearchDate><Name><Business>HYESTAR SEE U</Business><Full>HYESTAR SEE U</Full></Name><AddressList><Address><Type>Current</Type><Street1>74 BUSH AVE</Street1><City>NORTH HOLLYWOOD</City><State>CA</State><PostalCode>91605</PostalCode></Address></AddressList><IDList><ID><Type>AccountID</Type><Number>1</Number></ID></IDList><PhoneNumberList><Phone><Type>Business</Type><Number>2034444444</Number></Phone></PhoneNumberList></RecordDetailInfo></Record></RecordList></SearchResult></SearchResponse></soap:Body></soap:Envelope>";
DataAccessLayer.ExecuteNonQuery(KYCDAL.DataAccessLayer.ProcedureNames.PRC_SAVE_CACHE_LOG.ToString(), "cacheName_", "KYC", "applicationName_", "Testing Output: " + soapMsg1, "True", "accessKey_");
writeStr.Write(soapMsg1);
writeStr.Flush();
inwardStream.Position = 0;
break;
case SoapMessageStage.AfterSerialize:
inwardStream.Position = 0;
readStr = new StreamReader(inwardStream);
writeStr = new StreamWriter(outwardStream);
soapMsg1 = readStr.ReadToEnd();
DataAccessLayer.ExecuteNonQuery(KYCDAL.DataAccessLayer.ProcedureNames.PRC_SAVE_CACHE_LOG.ToString(), "cacheName_", "KYC", "applicationName_", "Bridger Input: " + soapMsg1, "True", "accessKey_");
writeStr.Write(soapMsg1);
writeStr.Flush();
break;
}
}

}

[AttributeUsage(AttributeTargets.Method, AllowMultiple = true, Inherited = true)]
public class ServerExtensionAttribute : SoapExtensionAttribute
{
private int priority = 1;
public override Type ExtensionType
{
get { return typeof(WebServcSoapExt); }
}

public override int Priority
{
get { return priority; }
set { priority = value; }
}
}

fgfg

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.