我如何解析json对象?

时间:2012-07-24 13:31:35

标签: .net regex json

我目前正在尝试从我们从Web服务器访问我们的应用程序的文件中过滤一些信息

信息当前的格式为:

{"ns2":"ns2.xxxx.xxx","ns1":"ns1.xxxx.xxx","classname":"com.logicboxes.foundation.sfnb.order.domorder.DomCno","privacyprotectedallowed":"true","isOrderSuspendedUponExpiry":"false","noOfNameServers":"2","isprivacyprotected":"true","classkey":"domcno","isImmediateReseller":"false"}

我们想要做的是逐件提取信息,但我们试图从NS2和NS1字段开始

在我们有以下内容格式化我们的数据之前,我似乎无法改变它以使其过滤掉上面的字符串

Dim m As New Regex("(?<=<div id=""xxxx"">).+(?=</div>)")

我想知道这里是否有人可以给我一条下去的路

由于

1 个答案:

答案 0 :(得分:2)

在您的情况下,您可以执行以下操作:

您的JSON结果作为.net对象

string json = "{"ns2":"ns2.xxxx.xxx","ns1":"ns1.xxxx.xxx","classname":"com.logicboxes.foundation.sfnb.order.domorder.DomCno","privacyprotectedallowed":"true","isOrderSuspendedUponExpiry":"false","noOfNameServers":"2","isprivacyprotected":"true","classkey":"domcno","isImmediateReseller":"false"}";


public class Blob
{
      public string ns2 {get;set;}
      public string ns1 {get;set;}
      public string classname {get;set;}
      public bool privacyprotectedallowed {get;set;}
      public bool isOrderSuspendedUponExpiry{get;set;}
      public int noOfNameServers{get;set;}
      public bool isprivacyprotected{get;set;}
      public string classkey{get;set;}
      public bool isImmediateReseller{get;set;}
}

您可以使用此库进行转换:http://james.newtonking.com/projects/json-net.aspx

以下是例子:

var Blob = JsonConvert.DeserializeObject<Blob>(json);

希望这会帮助你!

我还建议您查看ContractResolvers,以便将PascalCasing用于您的属性。更多信息可以在here

找到

如果您不想创建Blob对象,也可以尝试:

JObject obj = JObject.Parse(json);
string ns1 = (string)obj["ns1"];
string ns2 = (string)obj["ns2"];