将json结果转换为我的c#模型 [英] convert json result to my c# model

查看:167
本文介绍了将json结果转换为我的c#模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的Json是
{header:
[start_ip_int,end_ip_int,start_ip_oct,end_ip_oct,continent,country, COUNTRY_CODE, country_cf, 区, 国家, STATE_CODE, state_cf, 城市, city_cf, POSTAL_CODE, AREA_CODE, TIME_ZONE, 纬度,经度, DMA, MSA, CONNECTION_TYPE, line_speed, ip_routing_type, ASN, SLD, 顶级域名, 组织, 载体, anonymizer_status, 家, organization_type,naics_code,isic_code],
results:{120.74.45.21:[2856990464,2856990719,140.74.65.0,370.64.55.255,北美,美国,我们,99,东北,纽约,纽约,80,纽约,61,10020,212 , - 5,40.75891, - 73.97902,501,35620,tx,高,固定,5049,,,院长金融服务,morgan stanley& co。,,false,Finance,520000,K6400
]
},timeSpent:6}









我想转换成型号。我得到了链接,我可以将json转换为合适的模型。







公共类RootObject 
{
公共列表< string < span class =code-keyword>> headers {get;组; }
public结果{get;组; }
public int timeSpent {get;组; }
}

公共类结果
{
public IList < string > IpAddress {get;组; }
}







但问题出在json结果

results:{120.74.45.21:



这是动态的

所以在Results类中属性IpAddress未正确映射。





 JavaScriptSerializer js = new JavaScriptSerializer(); 
var objText = reader.ReadToEnd();
RootObject obj1 = new RootObject();

结果obj = new Results();
obj.IpAddress.Add(ipAddress);
obj1.results = obj;
obj1 =(RootObject)js.Deserialize(objText,typeof(RootObject));




obj1中的
我没有得到结果。我怎样才能解决这个问题。需要你的建议





否则请帮助我如何实现这个目标

我的要求:

在json中我们有标题和回复

i想要用相应的响应映射标题。

在当前情况下



start_ip_int:2856990464 
end_ip_int:2856990719
start_ip_oct:140.74.65.0
end_ip_oct:370.64.55.255
大陆:北美洲

解决方案

请查看我过去的答案:

如何将对象类型转换为C#类对象类型 [ ^ ],

如何将多级json数据转换为C#对象? [ ^ ]。



-SA

My Json is 
{"headers":
["start_ip_int","end_ip_int","start_ip_oct","end_ip_oct","continent","country","country_code","country_cf","region","state","state_code","state_cf","city","city_cf","postal_code","area_code","time_zone","latitude","longitude","dma","msa","connection_type","line_speed","ip_routing_type","asn","sld","tld","organization","carrier","anonymizer_status","home","organization_type","naics_code","isic_code"],
"results":{"120.74.45.21":["2856990464","2856990719","140.74.65.0","370.64.55.255","north america","united states","us","99","northeast","new york","ny","80","new york","61","10020","212","-5","40.75891","-73.97902","501","35620","tx","high","fixed","5049","","","dean witter financial services","morgan stanley & co.","","false","Finance","520000","K6400"
]
},"timeSpent":6}





I wanted to convert into model. I got the link where i can convert the json to appropriate model.



public class RootObject
   {
       public List<string> headers { get; set; }
       public Results results { get; set; }
       public int timeSpent { get; set; }
   }

   public class Results
   {
       public IList<string> IpAddress { get; set; }
   }




But the problem is in json result

"results":{"120.74.45.21":


this is dynamic
so in Results class the property IpAddress is not mapping correctly.


JavaScriptSerializer js = new JavaScriptSerializer();
                            var objText = reader.ReadToEnd();
                            RootObject obj1 = new RootObject();

                            Results obj = new Results();
                            obj.IpAddress.Add(ipAddress);
                            obj1.results = obj;
                            obj1 = (RootObject)js.Deserialize(objText, typeof(RootObject));



in obj1 i am not getting the results. how can i fix this. Need your suggestion


otherwise kindly help me how i can achieve this
My requirement:
in json we have header and response
i want to map header with the the corresponding response.
in current case

"start_ip_int" :  2856990464
"end_ip_int"   :  2856990719
"start_ip_oct" :  140.74.65.0
"end_ip_oct"   :  370.64.55.255  
"continent"    :  north america

解决方案

Please see my past answers:
How To Convert object type to C# class object type[^],
how to conver multi level json data to C# Object?[^].

—SA


这篇关于将json结果转换为我的c#模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆