I'm trying to produce the following JSON structure with JavaScriptSerializer in Net 3.5:
{
"chart": "pie",
"series": [
{
"name": "Browsers",
"data": [
[
"Firefox",
6
],
[
"MSIE",
4
]
],
"size": "43%"
}
]
}
The issue I face is regarding the data section. I cannot seem to create a class that serializes into the above structure in data.
I have read other posts which kind of touch the subject but the solution always seem to be to use JSON.Net. For instance Serializing dictionaries with JavaScriptSerializer. Although, I primaily want to solve it by using the JavaScriptSerializer or other built-in features in the .Net 3.5 framework.
How do I produce the data section?
*EDIT The way to solve it with the JavaScriptSerializer is to create an object array for the data property on the object about to be serialized.
Input parameter data is "Firefox:6\nMSIE:4\nChrome:7".
public object[] GetData(string data)
{
var dataStrArr = data.Split(new[] { Environment.NewLine }, StringSplitOptions.None);
var dataArr = new object[dataStrArr.Length];
for (var index = 0; index < dataStrArr.Length; index++)
{
var component = dataStrArr[index];
var componentArr = component.Split(':');
if (componentArr.Length != 2)
continue;
var key = componentArr[0];
decimal value;
if (decimal.TryParse(componentArr[1], out value))
{
var keyValueArr = new object[2];
keyValueArr[0] = key;
keyValueArr[1] = value;
dataArr[index] = keyValueArr;
}
}
return dataArr;
}
I think you need something like the following class (RootObject) to serialize that:
public class Series
{
public string name { get; set; }
public List<List<object>> data { get; set; }
public string size { get; set; }
}
public class RootObject
{
public string chart { get; set; }
public List<Series> series { get; set; }
}
Let me know if that helps.
Related
Returns Json format data to me from an api.
However, the type of the "FuelType" property inside the object may be different. For example, 1 time comes as follows:
{
...
fuelType: "gasoline"
...
}}
But then it can happen:
{
...
fuelType: ["gasoline", "any"]
...
}}
If I set the "FuelType" property type on my model to a string, in the second case, Json will give me an error when it arrives, because it can't convert from array to string. No, if I set the type to an array, then, conversely, if a string arrives, it will issue an error because it cannot convert from a string to an array.
In this case, what should I put the "FuelType" property type in my model so that it does not make an error when deserializing?
It all depends on you,what type of value you want to receive?string or List?
I tried with the codes:
public class SomeModel
{
public SomeModel()
{
fuelType = new List<string>();
}
public int Id { get; set; }
public string Name { get; set; }
public List<string> fuelType { get; set; }
//you could move the codes to someservice
public List<string> someresult(string a)
{
var targetlist = new List<string>();
targetlist.Add(a);
return targetlist;
}
public List<string> someresult(List<string> a)
{
var targetlist = new List<string>();
targetlist.AddRange(a);
return targetlist;
}
}
[HttpPost]
public JsonResult SomeAction()
{
var somemodel = new SomeModel() { Id = 1, Name = "name" };
var somevalue = /*"a"*/new List<string>() { "a", "b", "c" };
var targetvalue = somemodel.someresult(somevalue);
somemodel.fuelType.AddRange(targetvalue);
return new JsonResult(somemodel);
}
Result:
try this
var fuelType = JsonConvert.DeserializeObject<MyClass>(json);
public class MyClass
{
[JsonProperty("fuelType")]
private JToken _fuelType;
[JsonIgnore]
public string[] fuelType
{
get {
if (_fuelType==null) return null;
return _fuelType is JArray ? _fuelType.ToObject<string[]>() : new string[] { (string)_fuelType }; }
set { _fuelType = JsonConvert.SerializeObject(value); }
}
}
I've been tasked will calling an API, that needs to take a JSON payload as its request.. An example format of the JSON is as follows:
{
"method":"methodname",
"in":[
{
"account":"acme",
"context":"abc123"
},
"content",
{
"mSearchText":"chocolate",
"mItemDataIDs":[
"Entry:ID",
"Entry:EntryRef",
"Entry:CategoryID"
]
}
]
}
I am using JSON.NET (Newstonsoft) to construct my JSON from .net objects.
The issue I am facing is correctly constructing the "in" section..
It appears to be an array of objects, but only the second item has a title ("content",{.....})..
The closest I can get is:
{
"method": "methodname",
"in":[
{
"account": "PANDO",
"context": "sdfsd22342"
},
{
"mSearchText":"chocolate",
"mItemDataIDs":[
"Entry:ID",
"Entry:EntryRef",
"Entry:CategoryID"
]
}
]
}
Which is identical apart from "content", is missing:
My code so far is:
public class Payload
{
public string method { get; set; }
[JsonProperty("in")]
public List<object> Items { get; set; }
}
public class AccountDetails
{
public string account { get; set; }
public string context { get; set; }
}
[JsonObject(Title = "content")]
public class Content
{
public string mSearchText { get; set; }
public string[] mItemDataIDs { get; set; }
}
Payload payload = new Payload();
payload.method = "methodname";
payload.Items = new List<object>();
payload.Items.Add(new AccountDetails
{
account = "acme",
context = "abc123"
});
Content conent = new Content
{
mSearchText = "chocolate",
mItemDataIDs = new string[] { "Entry:ID", "Entry:EntryRef", "Entry:CategoryID" }
};
payload.Items.Add(conent);
string jsonObject = JsonConvert.SerializeObject(payload, Formatting.Indented);
Any suggestions on what I can do?
I have a question about inserting a json formatted string into a json structure and having the final version be a combined JSON formatted string that can be serialized into a JSON structure. I am using the newtonsofts Json.NET
I have the following JSON structure:
public class ResponseJson
{
[JsonProperty(PropertyName = "header")]
public ResponseHeader responseHeader { get; set; }
[JsonProperty(PropertyName = "results")]
public string responseResults { get; set; }
}
public class ResponseHeader
{
[JsonProperty(PropertyName = "name")]
public string name { get; set; }
[JsonProperty(PropertyName = "version")]
public string version { get; set; }
}
In the code, I do the following:
ResponseJson responseJson = new ResponseJson();
responseJson.responseHeader = new ResponseHeader()
{
name = A_NAME,
version = A_VERSION
};
responseJson.responseResults = resultJson;
return JsonConvert.SerializeObject(responseJson, new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore });
resultJson is a properly formatted JSON string (in this case, an array of objects, but could be anything JSON formatted). Right now, if i execute the code, I get the following (which is expected, since "results" is declared as a string):
{
"header":
{
"name":"abcd",
"version":"1.0"
},
"results":
"[{\"data\":{\"level\":\"100\"},\"code\":{\"value\":\"JBC\",\"type\":\"ev\"},\"time\":{\"start\":\"20\",\"end\":\"101\"}}]"
}
what I do need as an output is:
{
"header":
{
"name":"abcd",
"version":"1.0"
},
"results":
[
{
"data":
{
"level":"100"
},
"code":
{
"value":"JBC",
"type":"ev"
},
"time":
{
"start":"20",
"end":"101"
}
}
]
}
While you don't explain how you create your resultJson variable, it's implied from your code that you are double-serializing your results: you compute an array of "result" classes, serialize them to a JSON string, store the string in your ResponseJson class, and them serialize that in turn. The embedded JSON string then gets escaped as per the JSON standard, which is what you are seeing.
You need to avoid double-serializing your data, for instance by using the following data model:
public class ResponseJson<T>
{
[JsonProperty(PropertyName = "header")]
public ResponseHeader responseHeader { get; set; }
[JsonProperty(PropertyName = "results")]
public T responseResults { get; set; }
}
public static class ResponseJson
{
public static ResponseJson<T> Create<T>(T responseResults, ResponseHeader responseHeader)
{
return new ResponseJson<T> { responseResults = responseResults, responseHeader = responseHeader };
}
}
public class ResponseHeader
{
[JsonProperty(PropertyName = "name")]
public string name { get; set; }
[JsonProperty(PropertyName = "version")]
public string version { get; set; }
}
Then you would do:
var results = GetResults(); // Get your result data
var responseJson = ResponseJson.Create(results, new ResponseHeader()
{
name = A_NAME,
version = A_VERSION
});
return JsonConvert.SerializeObject(responseJson, new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore });
If for whatever reason you must embed a previously serialized JSON string as JSON rather than as a string literal, you'll need to re-parse it back to a JToken:
string resultJson = GetResultJson(); // Get result json string.
var responseJson = ResponseJson.Create(JToken.Parse(resultJson), new ResponseHeader()
{
name = A_NAME,
version = A_VERSION
});
return JsonConvert.SerializeObject(responseJson, new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore });
Json.NET will include the JToken in your outer JSON as nested JSON rather than as a string literal.
I have the following ActionResult:
public ActionResult WeatherWidget()
{
string json = string.Empty;
using (var client = new WebClient())
{
json = client.DownloadString("http://api.wunderground.com/api/xxxxxxxxx/geolookup/conditions/forecast/q/Australia/sydney.json");
}
WeatherWidget weatherWidget = new WeatherWidget()
{
//What do I put in here?
};
return View(weatherWidget);
}
And the following model:
public class WeatherWidget
{
public string city { get; set; }
public string lat { get; set; }
public string lon { get; set; }
}
Here is a snippet of the json:
{
"response": {
"version":"0.1",
"termsofService":"http://www.wunderground.com/weather/api/d/terms.html",
"features": {
"geolookup": 1
,
"conditions": 1
,
"forecast": 1
}
}
,"location": {
"type":"INTLCITY",
"country":"AU",
"country_iso3166":"AU",
"country_name":"Australia",
"state":"VC",
"city":"Falls Creek",
"tz_short":"AEST",
"tz_long":"Australia/Melbourne",
"lat":"-36.86999893",
"lon":"147.27000427",
"zip":"00000",
How do I populate the model to display in my view?
e.g.. #Model.city or #Html.Raw(Model.city)
I have no problem displaying the data via Javascript in my view and I can do this with XML and Html using the HtmlAgilityPack, I just can't work out how to do it with Json.
Add Newtonsoft.Json via NuGet, add the following using statements
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
and then use the following code to extract the data out of your JSON response.
JObject parsedJson = (JObject)JsonConvert.DeserializeObject(json);
JObject location = (JObject)parsedJson["location"];
WeatherWidget weatherWidget = new WeatherWidget();
weatherWidget.city = location["city"];
weatherWidget.lat = location["lat"];
weatherWidget.lon = location["lon"];
I need to turn this beutifull default JSON from Service Stack :
{
"Status": "ok",
"LanguageArray": [{
"Id": 1,
"Name": "English"
}, {
"Id": 2,
"Name": "Chinese"
}, {
"Id": 3,
"Name": "Portuguese"
}]
}
To this monstrosity of abomination:
{"status":"ok","language":{
"0":{"id":"1", "name":"English"},
"1":{"id":"2", "name":"Chinese"},
"2":{"id":"3", "name":"Portuguese"},
}
For reasons beyond my control or ration or logic.
My question is what are the simplest way of achieving this?
I need to do this to almost all Arrays ( they are IEnumerables implemented using List in C#)
I don't think I have a 'simplest way' and it kind of depends on where you need the serialization to happen as well as how strict the format is (case sensitive?, spacing?)
If place your example text and turn it into classes like this
public class Language
{
public int Id { get; set; }
public string Name { get; set; }
}
public class OuterLanguage
{
public string Status { get; set; }
public IEnumerable<Language> LanguageArray { get; set; }
}
A pretty straightforward way would be having a Dto that adds a property for serialization of the Array and ignores the original Array.
public class OuterLanguageDto
{
public string Status { get; set; }
[IgnoreDataMember]
public IEnumerable<Language> LanguageArray { get; set; }
public IDictionary<string, Language> language
{
get { return this.LanguageArray.ToDictionary(x => (x.Id - 1).ToString()); }
}
}
When you need to serialize your class (OuterLanguage) you use TranslateTo (or your preferred Mapper) and copy the values over to the Dto.
var jsonObj = obj.TranslateTo<OuterLanguageDto>();
var jsonStr = new JsonSerializer<OuterLanguageDto>().SerializeToString(jsonObj);
You could also look into Custom Serialization. That would give you to have a way to serialize all your Arrays into the correct format.
JsConfig<IEnumerable<Language>>.SerializeFn = ToJson
private string ToJson(IEnumerable<Language> arr)
{
var dict = arr.ToDictionary(x => x.Id - 1);
var str = JsonSerializer.SerializeToString<Dictionary<int, Language>>(dict);
return str;
}
However, I'm guessing you may have to do some "magic string work" to take your custom serialized Array's json string and format it properly when outer class (OuterLanguage in this example) is the one being serialized.