我正在尝试使用c#2.0编写一个控制台应用程序,它将使用我的web服务并从web方法返回结果。
在我的C#代码中,我可以从网络方法中获取我的值,请参阅下面的示例代码:
// print results
try
{
Type objtype = Type.GetType(crisresult.ToString());
object obj = Activator.CreateInstance(objtype);
Object[] mArgs = new Object[methodArgs.Length + 1];
methodArgs.CopyTo(mArgs, 0);
mArgs.SetValue(obj, methodArgs.Length);
methodArgs = mArgs;
Object result = mi.Invoke(service, methodArgs);
Console.WriteLine(result);
}
catch (Exception e)
{
Console.WriteLine("Error invoking method '" + methodName + "'");
Console.WriteLine(e);
}
Console.WriteLine("Press enter to continue...");
Console.ReadLine();
现在在上面的代码中,我的webmethod的结果在JSON Type中完全返回,因此我的Object result
具有JSON类型的值,例如:
{"FullName":"Mr Mahesh Sharma","Miles":0,"TierStatus":"IO","TierMiles":0,"MilesExpiry":0,"ExpiryDate":"30/03/2012 00:00:00","AccessToken":"106C9FD143AFA6198A9EBDC8B9CC0FB2CE867356222D21D45B16BEEB9A7F390B5E226665851D6DB9","ActiveCardNo":"00300452124","PersonID":8654110}
以上结果,我想在控制台中打印以下格式:
FullName: Mr Mahesh Sharma
Miles: 0
TierStatus: IO
TierMiles:0
MilesExpiry:0
ExpiryDate:31 March 2012
AccessToken: 106C9FD143AFA6198A9EBDC8B9CC0FB2CE867356222D21D45B16BEEB9A7F390B5E226665851D6DB9
ActiveCardNo: 00300452124
PersonID: 8654110
答案 0 :(得分:17)
我个人使用JSON.NET。示例代码:
using System;
using Newtonsoft.Json.Linq;
class Test
{
static void Main()
{
string json = "{\"FullName\":\"Mr Mahesh Sharma\",\"Miles\":0,\"TierStatus\":\"IO\"," +
"\"TierMiles\":0,\"MilesExpiry\":0,\"ExpiryDate\":\"30/03/2012 00:00:00\"," +
"\"AccessToken\":\"106C9FD143AFA6198A9EBDC8B9CC0FB2CE867356222D21D45B16BEE" +
"B9A7F390B5E226665851D6DB9\",\"ActiveCardNo\":\"00300452124\",\"PersonID\":8654110}";
JObject parsed = JObject.Parse(json);
foreach (var pair in parsed)
{
Console.WriteLine("{0}: {1}", pair.Key, pair.Value);
}
}
}
输出:
FullName: "Mr Mahesh Sharma"
Miles: 0
TierStatus: "IO"
TierMiles: 0
MilesExpiry: 0
ExpiryDate: "30/03/2012 00:00:00"
AccessToken: "106C9FD143AFA6198A9EBDC8B9CC0FB2CE867356222D21D45B16BEEB9A7F390B5E226665851D6DB9"
ActiveCardNo: "00300452124"
PersonID: 8654110
请注意,每对中的值为JToken
,您可以将其转换为相关类型。
答案 1 :(得分:17)
您可以使用以下内容:
return Newtonsoft.Json.JsonConvert.SerializeObject( obj, Formatting.Indented );
简单干净。
答案 2 :(得分:1)
如果您返回的json始终采用该格式,那么您也可以尝试使用JavaScriptSerializer
请尝试以下代码:
class Result
{
public string FullName{get; set;}
public int Miles {get;set;}
public string TimerStatus {get;set;}
public int TimerMiles {get;set;}
public int MilesExpiry {get;set;}
public DateTime ExpiryDate{get;set;}
public string AccessToken {get;set;}
public string ActiveCardNo {get;set;}
public long PersonID {get;set;}
}
然后将字符串反序列化为C#对象。
JavaScriptSerializer jsr = new JavaScriptSerializer();
Result res = jsr.Deserialize<Result>(/*your json string*/);
Console.Writeline("FullName:{0}\n"+"
"Miles{1}\n" +
"TimerStatus:{2}\n"+
"TimerMiles:{3}\n"+
"MilesExpiry:{4}\n"+
"ExpiryDate:{5}"+
"AccessToken:{6}"+
"ActiveCardNo:{7}"+
"PersonID:{8}",
res.FullName, res.Miles,res.TimerStatus,res.TimerMiles,res.MilesExpiry,res.Expirydate,res.AccessToken,res.ActivecardNo,res.PersonID
)
根据需要输出:
FullName: Mr Mahesh Sharma
Miles: 0
TierStatus: IO
TierMiles:0
MilesExpiry:0
ExpiryDate:31 March 2012
AccessToken: 106C9FD143AFA6198A9EBDC8B9CC0FB2CE867356222D21D45B16BEEB9A7F390B5E226665851D6DB9
ActiveCardNo: 00300452124
PersonID: 8654110