【问题标题】:How to return WebRequest Multidimensional JSON?如何返回 WebRequest 多维 JSON?
【发布时间】:2018-03-06 20:57:39
【问题描述】:

这是我当前的代码:

    public string Get(int id)
    {
        HttpRequestCachePolicy policy = new HttpRequestCachePolicy(HttpRequestCacheLevel.Default);
        HttpWebRequest.DefaultCachePolicy = policy;
        HttpRequestCachePolicy noCachePolicy = new HttpRequestCachePolicy(HttpRequestCacheLevel.NoCacheNoStore);
        HttpWebRequest request = (HttpWebRequest)WebRequest.Create("some URl that returns a json");
        request.Method = WebRequestMethods.Http.Get;
        request.Accept = "application/json";
        request.ContentType = "application/json; charset=utf-8";
        request.MaximumAutomaticRedirections = 4;
        request.MaximumResponseHeadersLength = 4;
        request.Credentials = CredentialCache.DefaultCredentials;
        request.CachePolicy = noCachePolicy;
        HttpWebResponse response = (HttpWebResponse)request.GetResponse();

        return // RESPONSE AS JSON???;
    }

    public class Persons
    {
        public string index { get; set; }
        public string thing { get; set; }
        public string name { get; set; }
        public string title { get; set; }
    }

我想将响应返回为多维 json..

我该怎么做? 它应该是这样的:

{"data":{"Person1":{"id":1,"thing":"thingOne","name":"personOneName","title":"personOneTitle"},"Person2":{"id":2,"thing":"thingTwo","name":"personTwoName","title":"personTwoTitle"}}

【问题讨论】:

  • 多维?它看起来像一个带有“数据”的对象,它是一个 Person 数组,尽管不是有效的 JSON。您可以只返回Person 的序列化集合并放弃“数据”字段或使用“数据”字段创建包装类。
  • 也许我在谈论其他事情.. 所以最后 json 必须看起来像这样 {"data":{"Person1":{"id":1,"thing":"thingOne ","name":"personOneName","title":"personOneTitle"},"Person2":{"id":2,"thing":"thingTwo","name":"personTwoName","title": "personTwoTitle"}}, "type":"Person","version":"1.1.0"} httprequest 的响应给出了未定义的人员数量,我需要将它们作为 javascript 的有效 json 返回,我不知道如何.

标签: c# json http


【解决方案1】:

请务必将以下指令添加到您的课程中

using Newtonsoft.Json; 

此代码会将您的响应转换为对象。

Stream newStream = response .GetResponseStream();
StreamReader sr = new StreamReader(newStream);    
var result = sr.ReadToEnd();    

//this will turn your response into a c# object of RootObject Type
//only do this is you're sure it will Deserialize into a RootObject type. 
var convertResponseToObj= JsonConvert.DeserializeObject<RootObject>(result); 

//if you want to see what's being returned by your endpoint 
//without turning it into a RootObject type then remove <RootObject> see line below.
//doing this may help you Deserialize the response correctly. 

//var convertResponseToObj= JsonConvert.DeserializeObject(result); 

public class Persons
{
     public string index { get; set; }
     public string thing { get; set; }
     public string name { get; set; }
     public string title { get; set; }
}

public class RootObject
{
     public List<Person> data {get; set;}
}

如果您想对该 c# 对象进行一些操作,然后将其转换为 JSON,您可以这样做:

string myJson = JsonConvert.SerializeObject(someCSharpObj); //this will turn your c# object into json 

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-05-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-02
    • 2018-10-20
    • 1970-01-01
    相关资源
    最近更新 更多