1

I have some problems with NullReferenceException by JSON print. I would like to parse dynamically created JSON code, and write out the value of them.

This is an example JSON that I want to parse with System.Json(Object.Parse) (these JSONs are created by PHP code, and it is not always six items long, "db" gives the size of the list):


{
    "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": "/"
            }
        }
    }
}

So I want to get all of the values from a1 to a6 with a for loop. I tried this code (and so much else), but it doesn't work. I get:

Microsoft.CSharp.RuntimeBinder.RuntimeBinderException -> Cannot perform runtime binding on a null reference

or

"NullReferenceException" -> Object reference not set to an instance of an object.

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();
    }

What can I do?

4

1 回答 1

0

我已经做到了 :) 我将 JSON 数据更改为“文件”:[ {“名称”:“abc”等...并应用了一个 foreach 循环 –

于 2014-07-10T11:40:59.217 回答