1

尝试在 Facebook 上搜索页面

    if (sq.ObjectType.Equals("page"))
    {
        searchPath = "/search";

        req.q = sq.Query;
        req.type = sq.ObjectType;

    }

    dynamic results = FBClient_.Get(req);

而且我不知道如何解析结果。我不能只是简单地循环它们。

而且我也看不到该结构,因为它是动态对象,它具有以下结构:

{
   "data": [
      {
         "name": "Platform-3",
         "category": "Health/medical/pharmacy",
         "id": "362034114769"
      },
      {
         "name": "Platform Expos",
         "category": "Product/service",
         "id": "521000451259682"
      },
      {
         "name": "eXo Platform",
         "category": "Software",
         "id": "152603664817327"
      },
      {
         "name": "Platform 28",
         "category": "Bar",
         "id": "104411956289378"
      },
}

但如何得到它我不确定。如何将其转换为可读格式?

4

2 回答 2

1

这里有一些映射。

objects => IDictionary<string, object> or IDictionary<string, dynamic>
arrays => IList<object> or IList<dynamic>
number => long if whole number, double if have decimal values
string => string
boolean => bool

所以在你的情况下你可以这样做。

dynamic result = fb.Get("...")
foreach(var data in result.data) {
    var name = data.name;
    var category = data.category;
    var id = data.id
}

由于result.data是动态的,实际上IList<object>您可以在其上使用 foreach 。你也可以试试这个。

dynamic result = fb.Get("...")
IList<dynamic> data = result.data;
foreach(var d in data) {
    string name = d.name;
    string category = d.category;
    string id = d.id
}

或者你是强类型的。

public class SearchResults {
    public IList<SearchResult> data { get; set;}
}

public class SearchResult {
    public string id { get; set; }
    public string name { get; set; }
    public string category { get; set; }
}

var result = fb.Get<SearchResults>(...)

如果您想遵循 C# 命名标准,请使用DataContractDataMember.

[DataContract]
public class SearchResults {
    [DataMember(Name = "data")]
    public IList<SearchResult> Data { get; set;}
}

[DataContract]
public class SearchResult {
    [DataMember(Name = "id")]
    public string Id { get; set; }

    [DataMember(Name = "Name")]
    public string Name { get; set; }

    [DataMember(Name = "Category")]
    public string Category { get; set; }
}
于 2012-11-30T03:34:57.963 回答
0

您要么必须创建强类型类来表示 prabir 发布的 JSON 数据,要么可以使用 JSON 库(例如Json.NET),它可以让您更轻松地与数据交互。这是他们页面中的一个示例:

string json = @"{
  ""Name"": ""Apple"",
  ""Expiry"": new Date(1230422400000),
  ""Price"": 3.99,
  ""Sizes"": [
    ""Small"",
    ""Medium"",
    ""Large""
  ]
}";

JObject o = JObject.Parse(json);

string name = (string)o["Name"];
// Apple

JArray sizes = (JArray)o["Sizes"];

string smallest = (string)sizes[0];
于 2012-11-30T03:39:18.637 回答