My favorites | Sign in
Project Home Downloads Wiki Issues Source
Checkout   Browse   Changes  
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
´╗┐using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Net.Json;
/// <summary>
/// Summary description for JsonParser
/// </summary>
public class JsonParser
{
JsonTextParser parser = null;
JsonObject jObject = null;

/// <summary>
/// Constructor
/// </summary>
public JsonParser()
{
parser = new JsonTextParser();
}

/// <summary>
/// Returns the Data Part of The JSON Script
/// </summary>
/// <param name="Array">JSON Script is an Array or List</param>
/// <param name="jsonText">Json Text</param>
/// <param name="breakOn">Data Required For</param>
/// <returns></returns>
public string getData(Boolean Array, string jsonText, string breakOn)
{
jObject = parser.Parse(jsonText);
int len = breakOn.Length;
len += 4;

string data = String.Empty;
if (Array)
{
foreach (JsonObjectCollection jObjectCollection in jObject as JsonArrayCollection)
{
foreach (JsonObject json in jObjectCollection)
{
string name = json.Name;
if (name.Equals(breakOn))
{
data = json.ToString();
data = data.Substring(len, data.Length - len);
break;
}
}
}
}
else
{
foreach (JsonObject json in jObject as JsonObjectCollection)
{
string name = json.Name;
if (name.Equals(breakOn))
{
string text = json.GetValue().ToString();
data = json.ToString();
data = data.Substring(len, data.Length - len);
break;
}
}
}
return data;
}

/// <summary>
/// Returns the Person Name
/// </summary>
/// <param name="jsonText">Json Text</param>
/// <returns></returns>
public string getName(string jsonText)
{
string lName = String.Empty;
string fName = String.Empty;

jObject = parser.Parse(jsonText);
foreach (JsonObject json in jObject as JsonObjectCollection)
{
string name = json.Name;
string value = json.GetValue().ToString();
if (name.Equals("familyName"))
lName = value;
else if (name.Equals("givenName"))
fName = value;
}
return fName + " " + lName;
}

/// <summary>
/// Returns Person Profile
/// </summary>
/// <param name="jsonText">Json Text</param>
/// <returns></returns>
public Person getProfile(string jsonText)
{
Person person = new Person();

jObject = parser.Parse(jsonText);
foreach (JsonObject json in jObject as JsonObjectCollection)
{
string name = json.Name;
string value = json.GetValue().ToString();

if (name.Equals("thumbnailUrl"))
person.ThumbnailUrl = value;
if (name.Equals("id"))
person.Id = value;
if (name.Equals("name"))
{
string nameData = getData(false, jsonText, "name");
person.Name = getName(nameData);
}
}
return person;
}

/// <summary>
/// Return Total Results
/// </summary>
/// <param name="jsonText">Json Text</param>
/// <returns></returns>
public int getCount(string jsonText)
{
jObject = parser.Parse(jsonText);

int messageCount = 0;
foreach (JsonObject json in jObject as JsonObjectCollection)
{
string name = json.Name;
if (name.Equals("totalResults"))
{
messageCount = int.Parse(json.GetValue().ToString());
break;
}
}
return messageCount;
}

/// <summary>
/// Return All Friends
/// </summary>
/// <param name="jsonText">Json Text</param>
/// <param name="no_of_friends">No. Of Friends</param>
/// <returns></returns>
public Person[] getFriends(string jsonText, int no_of_friends)
{
Person[] person = new Person[no_of_friends];
int count = 0;

jObject = parser.Parse(jsonText);
foreach (JsonObjectCollection json in jObject as JsonArrayCollection)
{
person[count++] = getProfile(json.ToString());
}

return person;
}

/// <summary>
/// Returns Message
/// </summary>
/// <param name="jsonText">Json Text</param>
/// <returns></returns>
public SocialMessage getMessage(string jsonText)
{
SocialMessage socialMessage = new SocialMessage();

jObject = parser.Parse(jsonText);

foreach (JsonObject json in jObject as JsonObjectCollection)
{
string name = json.Name;
string value = json.GetValue().ToString();
if (name.Equals("body"))
socialMessage.Message = value;
if (name.Equals("id"))
socialMessage.MessageId = value;
if (name.Equals("fromUserProfile"))
{
string personData = getData(false, jsonText, "fromUserProfile");
socialMessage.MPerson = getProfile(personData);
}
}

return socialMessage;
}

/// <summary>
/// Returns All Messages
/// </summary>
/// <param name="jsonText">Json Text</param>
/// <param name="no_of_messages">No. Of Messages</param>
/// <returns></returns>
public SocialMessage[] getMessages(string jsonText, int no_of_messages)
{
jObject = parser.Parse(jsonText);

SocialMessage[] socialMessage = new SocialMessage[no_of_messages];
int count = 0;

foreach (JsonObjectCollection json in jObject as JsonArrayCollection)
{
socialMessage[count++] = getMessage(json.ToString());
}

return socialMessage;
}
}

Change log

r2 by harshit.gangal on Jun 29, 2010   Diff
[No log message]
Go to: 
Project members, sign in to write a code review

Older revisions

All revisions of this file

File info

Size: 6570 bytes, 218 lines
Powered by Google Project Hosting