1
我有一些JSON打印NullReferenceException的問題。我想解析動態創建的JSON代碼,並寫出它們的值。C#NullReferenceException由JSON打印
這是我想System.Json(Object.Parse)
解析爲例JSON(這些JSONs是由PHP代碼創建的,它並不總是六個項目長,「DB」給出了列表的大小):
{
"crossclouds": {
"files": {
"db":"6",
"a1": {
"name": "list.txt",
"id": "2",
"size": "0",
"modif": "2012-05-17 23:10:33",
"given": "3vdcl7WXo5I0m9rcXontHEIQgf661JXS.txt",
"sec": "6To92mE15BO7",
"mappa": "/"
},
"a2": {
"name": "hexa2.png",
"id": "3",
"size": "0",
"modif": "2012-05-17 23:10:33",
"given": "Z87L6f84XUEtLVJ53g0T7ifYnnJ47UmE.png",
"sec": "iKX2bL9UsU98",
"mappa": "/"
},
....
"a6": {
"name": "kristof.txt",
"id": "24",
"size": "127",
"modif": "2012-05-17 23:10:33",
"given": "Ze5U3oi59CrkI4xTXLjUP07rilVa0jbS.txt",
"sec": "h25yaL8207n6",
"mappa": "/"
}
}
}
}
所以我想用一個for
循環得到從a1到a6的所有值。我試過這個代碼(還有其他的東西),但它不起作用。我得到:
Microsoft.CSharp.RuntimeBinder.RuntimeBinderException - >無法執行運行時的空引用
或
「的NullReferenceException」 結合 - >對象引用不設置到對象的實例。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
//using Newtonsoft.Json;
using System.Json;
using System.IO;
using System.Reflection;
namespace cc3
{
class Program
{
static void Main(string[] args)
{
Console.Write("Username: ");
string user = Console.ReadLine();
Console.Write("Password: ");
string pass = Console.ReadLine();
CookieContainer cookies = new CookieContainer();
ServicePointManager.Expect100Continue = false;
HttpWebRequest httpWReq = (HttpWebRequest)WebRequest.Create("http://XXXXX/fafa.php");
ASCIIEncoding encoding = new ASCIIEncoding();
string postData = "user=" + user;
postData += "&psw=" + pass;
byte[] data = encoding.GetBytes(postData);
httpWReq.CookieContainer = cookies;
httpWReq.Method = "POST";
httpWReq.ContentType = "application/x-www-form-urlencoded";
httpWReq.ContentLength = data.Length;
using (Stream newStream = httpWReq.GetRequestStream())
{
newStream.Write(data, 0, data.Length);
newStream.Close();
WebResponse response = httpWReq.GetResponse();
Console.WriteLine(((HttpWebResponse)response).StatusDescription);
Stream newStream2 = response.GetResponseStream();
StreamReader reader = new StreamReader(newStream2);
string responseFromServer = reader.ReadToEnd();
responseFromServer = responseFromServer.Replace("\n", "");
responseFromServer = responseFromServer.Replace("\r", "");
responseFromServer = responseFromServer.Replace("\t", "");
dynamic json = JsonValue.Parse(responseFromServer);
Console.WriteLine(json.crossclouds.files.db);
string db = json.crossclouds.files.db;
db = db.Replace("\"", "");
Console.WriteLine(json["db"]);
for (int i = 1; i <= Convert.ToInt16(db); i++)
{
string av = "a" + Convert.ToString(i);
object av2 = Convert.ToString("json.crossclouds.files." + av);
object r1 = (av2).GetType().GetProperty("name").GetValue(av2, null);
if (r1 == null)
{
Console.WriteLine("Unknown");
}
else
{
Console.WriteLine(r1);
}
}
}
reader.Close();
newStream2.Close();
response.Close();
}
Console.ReadLine();
}
我能做些什麼?
我已經做了:)我將JSON數據更改爲「files」:[{「name」:「abc」等等...並應用了一個foreach循環 – Ewert