java 调用webapi json_Java Post调用C#的webapi问题

public class JsonNetValueProviderFactory : ValueProviderFactory

{

public override IValueProvider GetValueProvider(ControllerContext controllerContext)

{

// first make sure we have a valid context

if (controllerContext == null)

throw new ArgumentNullException("controllerContext");

// now make sure we are dealing with a json request

if (!controllerContext.HttpContext.Request.ContentType.StartsWith("application/json", StringComparison.OrdinalIgnoreCase))

return null;

// use JSON.NET to deserialize object to a dynamic (expando) object

Object JSONObject;

// get a generic stream reader (get reader for the http stream)

using (StreamReader streamReader = new StreamReader(controllerContext.HttpContext.Request.InputStream))

{

// convert stream reader to a JSON Text Reader

using (JsonTextReader JSONReader = new JsonTextReader(streamReader))

{

// tell JSON to read

if (!JSONReader.Read())

return null;

// make a new Json serializer

JsonSerializer JSONSerializer = new JsonSerializer();

// add the dyamic object converter to our serializer

JSONSerializer.Converters.Add(new ExpandoObjectConverter());

// if we start with a "[", treat this as an array

if (JSONReader.TokenType == JsonToken.StartArray)

JSONObject = JSONSerializer.Deserialize>(JSONReader);

else

JSONObject = JSONSerializer.Deserialize(JSONReader);

}

}

// create a backing store to hold all properties for this deserialization

Dictionary backingStore = new Dictionary(StringComparer.OrdinalIgnoreCase);

// add all properties to this backing store

AddToBackingStore(backingStore, String.Empty, JSONObject);

// return the object in a dictionary value provider so the MVC understands it

return new DictionaryValueProvider(backingStore, CultureInfo.CurrentCulture);

}

private static void AddToBackingStore(Dictionary backingStore, string prefix, object value)

{

IDictionary d = value as IDictionary;

if (d != null)

{

foreach (KeyValuePair entry in d)

{

AddToBackingStore(backingStore, MakePropertyKey(prefix, entry.Key), entry.Value);

}

return;

}

IList l = value as IList;

if (l != null)

{

for (int i = 0; i 

{

AddToBackingStore(backingStore, MakeArrayKey(prefix, i), l[i]);

}

return;

}

backingStore[prefix] = value;

}

private static string MakeArrayKey(string prefix, int index)

{

return prefix + "[" + index.ToString(CultureInfo.InvariantCulture) + "]";

}

private static string MakePropertyKey(string prefix, string propertyName)

{

return (String.IsNullOrEmpty(prefix)) ? propertyName : prefix + "." + propertyName;

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值