Tag: json

使用ServiceStack使用REST Web服务时的用户身份validation

ServiceStack文档中有很多关于如何使用服务器端实现用户身份validation的示例。 但是如何在客户端设置用户凭据? 我使用ServiceStack来使用这样的JSON REST服务: var restClient = new JsonServiceClient (baseUri); var response = restClient.Get (“/some/service”); 如何在请求中添加任何forms的身份validation? 我想要使​​用的Web服务使用OAuth 1.0 ,但我也有兴趣添加自定义身份validation。 在我的代码中,我之前已经成功执行了OAuth令牌交换,因此我已经拥有了一个有效的访问令牌,现在需要使用此访问令牌及其token_secret对每个REST请求进行token_secret 。

如何将带有键/值的数组转换为JSON c#

我是C#的新手,我已经让自己头疼了。 我知道这可能是孩子们给你的东西,但我花了一个小时+谷歌周围,似乎无法解决它。 我要做的就是将数组转换为JSON。 我很了解PHP,所以这里是我正在尝试做的一个例子(在PHP中): $myarr=array(“key1″=>”value for key 1″,”key2″=>”value for key 2”); $jsonArray=json_encode($myarr); 所以$jsonArray将是: {“key1″:”value for key 1″,”key2″:”value for key 2”} 现在,我正在努力做到这一点,但在C#中。 这是我到目前为止: String[] keys = new String[] { “emailSend”,”toEmail”}; String[] values = new String[] {textBox2.Text,textBox1.Text}; JavaScriptSerializer js = new JavaScriptSerializer(); string json = js.Serialize(keys);//final json result MessageBox.Show(json);//show me 我正在使用Visual Studio C#2010,它会抛出此错误(使用上面的代码): 找不到类型或命名空间名称’JavaScriptSerializer’(您是否缺少using指令或程序集引用?) 关于我在这里做错了什么的想法? 谢谢

我可以在j#中将json反序列化为匿名类型吗?

我从DB读了一个很长的json。 我只想要那个json的一个属性。 我有两个选择:a。 为该json创建一个接口并反序列化到该接口。 (因为我只需要一个属性,这是一种矫枉过正吗?)b。 找到我需要的子串(正则表达式?) 哪个是首选? 更新:我正在使用.net 3.5

返回带有小写首字母属性名称的json

我有LoginModel: public class LoginModel : IData { public string Email { get; set; } public string Password { get; set; } } 我有Web api方法 public IHttpActionResult Login([FromBody] LoginModel model) { return this.Ok(model); } 它返回200和身体: { Email: “dfdf”, Password: “dsfsdf” } 但是我希望能够获得较低的首字母属性 { email: “dfdf”, password: “dsfsdf” } 我有Json合同解析器进行纠正 public class FirstLowerContractResolver : DefaultContractResolver { protected […]

循环遍历c#中的json数组

我有一个json字符串, {“objectType” : “Subscriber”, “objectList”:[{“firstName”:”name1″,”email”:”email@example.com”,”address”:”exampleAddress”},{“firstName”:”name2″,”email”:”email2@example.com”,”address”:”exampleAddress2″}]} 我需要在我的C#代码中解析它。 我试过了, JavaScriptSerializer json_serializer = new JavaScriptSerializer(); object routes_list = json_serializer.DeserializeObject(myjson here); 但我无法循环“objectList”数组。 怎么做?

如何在asp.net web api中返回json错误消息?

我想返回一个json errormessage但是在fiddler中,我在json面板中看不到这个: string error = “An error just happened”; JsonResult jsonResult = new JsonResult { Data = error, JsonRequestBehavior = JsonRequestBehavior.AllowGet }; response = Request.CreateResponse(HttpStatusCode.BadRequest, jsonResult.Data); 这该怎么做?

从C#类生成JSON模式

有没有办法以编程方式从C#类生成JSON模式? 我们可以使用http://www.jsonschema.net/手动完成的事情

如何解析多态JSON数组?

我有一个JSON格式的文件,包含个人用户的记录。 一些用户的记录中间有一个注释字段。 我只想解析顶级项目(fullName contributorName email) 使用Newtonsoft.JSON解析器,但我似乎无法识别单个对象。 当我将整个字符串解析为一个大对象时,我不知道如何迭代各个字符串。 这就是我尝试这样做的方法(属性属性),但是如果它们出现故障或者有子属性则不起作用。 我需要将它放入一个对象中: StreamReader re = File.OpenText(“C:\\dropbox\\my dropbox\\clients\\towson\\english 317\\Ning Archive\\ning-members.json”); JsonTextReader reader = new JsonTextReader(re); string ct = “”; try { ct += “”; while (reader.Read()) { if (reader.TokenType == JsonToken.PropertyName) { if (reader.Value.ToString() == “fullName”) { reader.Read(); ct += “\r\n\r\n” + reader.Value + “”; } if (reader.Value.ToString() == […]

返回HttpResponseMessage的Web API最佳方法

我有一个Web API项目,我的方法总是返回HttpResponseMessage 。 所以,如果它工作或失败我返回: 没有错误: return Request.CreateResponse(HttpStatusCode.OK,”File was processed.”); 任何错误或失败 return Request.CreateResponse(HttpStatusCode.NoContent, “The file has no content or rows to process.”); 当我返回一个对象然后我使用: return Request.CreateResponse(HttpStatusCode.OK, user); 我想知道如何向HTML5客户端返回更好的封装respose,以便我可以返回有关事务的更多信息等。 我在考虑创建一个可以封装HttpResponseMessage但也有更多数据的自定义类。 有没有人实现类似的东西?

JSON使用多态对象数组进行反序列化

我遇到了涉及多态对象数组的JSON反序列化问题。 我已经尝试了这里和这里记录的序列化解决方案,这些解决方案非常适合序列化,但是它们都在反序列化方面有所作为。 我的class级结构如下: IDable [DataContract(IsReference=true)] public abstract class IDable { [DataMember] public T ID { get; set; } } 观察组 [DataContract(IsReference=true)] [KnownType(typeof(DescriptiveObservation))] [KnownType(typeof(NoteObservation))] [KnownType(typeof(NumericObservation))] [KnownType(typeof(ScoredObservation))] public class ObservationGroup : IDable { [DataMember] public string Title { get; set; } [DataMember] public List Observations { get; set; } [OnDeserializing] void OnDeserializing(StreamingContext context) { init(); } public […]