Sorunun kısa sürümü. Bazı durumlarda bir dize json değerine hiçbir tırnak istiyorum:Belirli değerlerin çevresinde alıntılar vermeden JSON nasıl serileştirilir
Renk = Highcharts.getOptions() renkler [0] bunun yerine
.
Renk = "Highcharts.getOptions(). renk [0]"
+++++++++++++++++++++++++++++++++++++++
Uzun ayrıntılar .... İstemcimde bir HighCharts.com grafiği için tüm Json'u üreten bir denetleyicinin (kavramın kanıtı olarak) oluşturduğum bir model olan çalışma kodum var.
Müşteri kodu (işleri)
@{
ViewBag.Title = "View";
}
<h2>High Charts Proof of Concept</h2>
<div id="container" style="min-width: 310px; height: 400px; margin: 0 auto"></div>
@section Scripts {
<script type="text/javascript">
$(function() {
fetchSampleChart1();
function fetchSampleChart1() {
$.ajax({
url: '/Test1/SampleChart1/',
type: 'GET',
//data: '?',
//data: 'adminEntityID=' + adminEntity + '&fieldName=' + fieldName + '&fieldValue=' + fieldValue + '&outletID=' + outlet,
complete: function (data, textStatus, xhr) {
console.log(data.responseText);
var strGraphData = $.parseJSON(data.responseText);
if (strGraphData.length == 0) {
//GraphEmptyDisplay(parmChartId, msg);
console.log("!!!!GRAPH DATA EMPTY!!!!");
return '';
}
$("#container").highcharts(strGraphData);
},
error:function(xhr, textStatus, errorThrown) {
//Inject a default error message to the modal target.
}
});
}
});
</script>
<script type="text/javascript" src="/scripts/jquery-1.10.2.min.js"></script>
<script type="text/javascript" src="https://code.highcharts.com/highcharts.js"></script>
<script type="text/javascript" src="https://code.highcharts.com/modules/exporting.js"></script>
}
Kontrolör Eylem onu çağırır. Bunun dışında çalışır, sadece çıktılarının çoğunda istediğim tüm çıktılarda alıntılar çıkarır.
:[HttpGet]
public ContentResult SampleChart1()
{
var sc = new HighChartGraph
{
Title = new Title() {Text = "Combination Chart"},
XAxis = new XAxis() {Categories = new string[] {"Apples", "Oranges", "Pears", "Banannas", "Plums"}},
Center = "[100,80]",
Size = 100,
ShowInLegend = false,
DataLabels = new dataLabels {Enabled = false},
Labels = new Labels
{
Items = new Items
{
Html = "Total Fruit consumption",
Style =
new Style()
{
Left = "50px",
Top = "18px",
//Color = "(Highcharts.theme && Highcharts.theme.textColor) || 'black'"
Color="yellow"
}
}
},
Series = new List<GraphSeries>
{
new GraphSeries
{
GraphType = "column",
Name = "Jane",
DataSimple = new decimal[] {3, 2, 1, 3, 4}
},
new GraphSeries
{
GraphType = "column",
Name = "John",
DataSimple = new decimal[] {2, 3, 5, 7, 6}
},
new GraphSeries
{
GraphType = "column",
Name = "Joe",
DataSimple = new decimal[] {4, 3, 3, 9, 0}
},
new GraphSeries
{
GraphType = "spline",
Name = "Average",
DataSimple = new decimal[] {3, 2.67m, 3, 6.33m, 3.33m},
Marker =
new marker
{
LineWidth = 2,
LineColor = "Highcharts.getOptions().colors[3]",
//LineColor="Black",
FillColor = "white"
}
},
new GraphSeries
{
GraphType = "pie",
Name = "Total Consumption",
DataExtended = new List<Data>
{
new Data {Name = "Jane", Y = 13, Color = "Highcharts.getOptions().colors[0]"},
//new data {Name = "Jane", Y = 13, Color = "Red"},
new Data {Name = "John", Y = 23, Color = "Highcharts.getOptions().colors[1]"},
//new data {Name = "John", Y = 23, Color = "Black"},
new Data {Name = "Joe", Y = 19, Color = "Highcharts.getOptions().colors[2]"}
//new data {Name = "Joe", Y = 19, Color = "Blue"},
}
}
}
};
//var j=new JavaScriptSerializer().Serialize(sc); // .NET Serialization will not use [JsonProperty()] attributes
var j = JsonConvert.SerializeObject(sc); // Newtonsoft Serialization WILL USE [JsonProperty()] attributes
//return Json(sc,JsonRequestBehavior.AllowGet); // cannot use it will not use our NewtonSoft serialization
return Content(j, "application/json");
}
3 modeli JSON için çağırır: i modeli ve örnek kod oluşturulan Highcharts grafik burada bir referans noktası olarak
public class HighChartGraph
{
[JsonProperty(PropertyName = "title")]
public Title Title;
[JsonProperty(PropertyName = "xAxis")]
public XAxis XAxis;
[JsonProperty(PropertyName = "labels")]
public Labels Labels;
[JsonProperty(PropertyName = "center")]
public string Center;
[JsonProperty(PropertyName = "size")]
public int Size;
[JsonProperty(PropertyName = "showInLegend")]
public bool ShowInLegend;
[JsonProperty(PropertyName = "dataLabels")]
public dataLabels DataLabels;
[JsonProperty(PropertyName = "series")]
public List<GraphSeries> Series;
}
public class Title
{
[JsonProperty(PropertyName = "text")]
public string Text;
}
public class XAxis
{
[JsonProperty(PropertyName = "categories")]
public string[] Categories;
}
public class Labels
{
[JsonProperty(PropertyName = "items")]
public Items Items;
}
public class Items
{
[JsonProperty(PropertyName = "html")]
public string Html;
[JsonProperty(PropertyName = "style")]
public Style Style;
}
public class Style
{
[JsonProperty(PropertyName = "left")]
public string Left;
[JsonProperty(PropertyName = "top")]
public string Top;
[JsonProperty(PropertyName = "color")]
public string Color;
}
public class GraphSeries
{
private string _GraphType;
[JsonProperty(PropertyName = "type")]
public string GraphType
{
get { return _GraphType; }
set { _GraphType = value; }
}
[JsonProperty(PropertyName = "name")]
public string Name;
[JsonProperty(PropertyName = "marker")]
public marker Marker;
[JsonProperty(PropertyName = "data")]
public object Data
{
get
{
if (DataSimple != null) return DataSimple;
if(DataExtended !=null) return DataExtended;
return null;
}
}
[NonSerialized][ScriptIgnore]
public decimal[] DataSimple = null;
[NonSerialized][ScriptIgnore]
public List<Data> DataExtended = null;
}
public class Data
{
[JsonProperty(PropertyName = "name")]
public string Name;
[JsonProperty(PropertyName = "y")]
public long Y;
[JsonProperty(PropertyName = "color")]
public string Color;
}
public class dataLabels
{
[JsonProperty(PropertyName = "enabled")]
public bool Enabled;
}
public class marker
{
[JsonProperty(PropertyName = "linewidth")]
public int LineWidth;
[JsonProperty(PropertyName = "linecolor")]
public string LineColor;
[JsonProperty(PropertyName = "fillcolor")]
public string FillColor;
}
Neden sadece 'return Json (sc, JsonRequestBehaviour.AllowGet) '? dizgeyi döndürmek ve istemci tarafında tekrar ayrıştırmak yerine? – Shyju
Kısa cevap: "Bazı durumlarda JSon değeri dizesinde tırnak istemiyorum:" geçerli JSON değil. JSON'u ayrıştıran hiçbir şey onu kabul etmeyecek. – jlbriggs
Well 'Color = Highcharts.getOptions() renkler [0]' JSON değil (en az geçerli JSON). Büyük olasılıkla özel bir seri hale getirici yazmanız gerekir. –