Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/336.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在c#,WP7中将对象转换为JSON字符串_C#_Json_Windows Phone 7 - Fatal编程技术网

在c#,WP7中将对象转换为JSON字符串

在c#,WP7中将对象转换为JSON字符串,c#,json,windows-phone-7,C#,Json,Windows Phone 7,我在尝试获取类似于的JSON字符串时遇到了问题 [{Key:{Key:value,Key:value,Key:value,Key:value}, 键:[{key:value,key:value,key:value,key:value}] 在C#中。 我的班级结构是这样的 public class wideeye { public listing listing { get; set; } public listing_images[] li

我在尝试获取类似于的JSON字符串时遇到了问题

[{Key:{Key:value,Key:value,Key:value,Key:value}, 键:[{key:value,key:value,key:value,key:value}]

在C#中。 我的班级结构是这样的

        public class wideeye
    {
        public listing listing { get; set; }
        public listing_images[] listing_images { get; set; }
    }

    public class listing
    {

        public string category { get; set; }
        public string city { get; set; }
        public string country { get; set; }
        public string created_at { get; set; }
        public string current_publish_date { get; set; }
        public string details { get; set; }
        public string id { get; set; }
        public string industry { get; set; }
        public string list_exp_date { get; set; }
        public string list_price { get; set; }
        public string list_start_date { get; set; }
        public string make { get; set; }
        public string model { get; set; }
        public string open_bid { get; set; }
        public string state { get; set; }
        public string status { get; set; }
        public string title { get; set; }
        public string updated_at { get; set; }
        public string year { get; set; }
    }

    public class listing_images
    {
        public string created_at { get; set; }
        public string id { get; set; }
        public string listing_id { get; set; }
        public string listing_image_content_type { get; set; }
        public string listing_image_file_name { get; set; }
        public int listing_image_file_size { get; set; }
        public string listing_image_updated_at { get; set; }
        public string updated_at { get; set; }

    }

}

代码是
清单1=
新上市{category=“electronics”,city=“BBSR”};
清单2=
新列表{created_at=“Instrumentation”,id=“10”};
List obid1=newlist(){bid1};
List obid2=新列表(){bid2};
//DataContractJsonSerializer序列化程序=null;
字符串sJSON=JsonConvert.SerializeObject(obid1);
字符串sJSONw=JsonConvert.SerializeObject(obid2);

本应将此作为注释,但相当麻烦:

在项目中添加对System.Web.MVC的引用,然后创建JSON,如下所示:

var jsonOutput = Json(obid1);
这就是我在MVC控制器中为AJAX调用生成JSON的方法。不过我还没有在Windows mobile应用程序中尝试过


只是想一想。

DataContractJsonSerializer类非常方便

将对System.Runtime.Serialization.dll和System.Servicemodel.Web.dll的引用添加到项目中

using System.Runtime.Serialization.Json;

...
...
...

    MemoryStream stream = new MemoryStream();
    DataContractJsonSerializer sr = new DataContractJsonSerializer(obj.GetType());

    sr.WriteObject(stream, obj);
    stream.Position = 0;

    StreamReader reader = new StreamReader(stream);
    string jsonResult = reader.ReadToEnd();

当然要做适当的异常处理。

我赞成微软采用JSON.net@Its,并且它比DataContractJsonSerializer更有效

 protected static string JsonSerialize(Object obj)
 {
        var serializer = new System.Web.Script.Serialization.JavaScriptSerializer { MaxJsonLength = int.MaxValue };
        var json = serializer.Serialize(obj);
        return json;
  }
此处的使用示例:

或者如果不使用Javascript序列化程序

 protected static string JsonSerialize(Object obj)
 {
        var serializer = new System.Web.Script.Serialization.JavaScriptSerializer { MaxJsonLength = int.MaxValue };
        var json = serializer.Serialize(obj);
        return json;
  }

上面代码中sJSON和sJSONw的值是什么?sJSON和aJSONw的值是[{key:value,key:value,key:value}]我可以有一个像[{“list”:{“category”:“Electronics”,“city”:“BBSR”,…},“listing_images”:[{“created_at::“Instrumentation”,“id:1,…}]这样的字符串吗?}我很难理解你的
[
s和
{
s与结束部分不匹配。使用上述代码,我为sJSON获取了2个json字符串,我为sJSON获取了字符串[{“类别”:“电子”、“城市”:“BBSR”、…}],为sJSON获取了字符串[{“cr”‌但是我需要一个组合这两个字符串的字符串,比如[{“列表”:{“类别”:“电子”,“城市”:“BBSR”,…},“列表图像”:[{“cr”‌​在“:“Instrumentation”,“id”:1,…}]}]处创建。希望您得到这个。thanx在advance@Jrsahoo参考您的评论…为什么不构建第三个类,它将包含两个列表作为成员,然后创建该类的对象并序列化它?