[英]convert array of json to javaScript object
I had problem i need to generate a js object from array of json objects using c# ie this is json :- 我有问题我需要使用c#从json对象数组生成js对象,即这是json:-
[
{
"FieldName": "lastPrice",
"LookupID": 1,
"FieldAr": "اخر سعر",
"FieldEn": "last price"
},
{
"FieldName": "currentPrice",
"LookupID": 2,
"FieldAr": "السعر الحالي",
"FieldEn": "current price"
}
]
and what I need is : 我需要的是:
var localAr = {};
local.lastPrice /*field name*/ = "اخر سعر"; // fieldAr
var localEn = {};
local.lastPrice /*field name*/ = "last price"; //FieldEn
I have no idea how to do this or even what i supposed search for 我不知道如何做到这一点,甚至不知道我应该搜索什么
You can do this using square-bracket notation, which allows you to use a string as a property name 您可以使用方括号符号来执行此操作,该方括号符号允许您将字符串用作属性名称
var input = [ { "FieldName": "lastPrice", "LookupID": 1, "FieldAr": "اخر سعر", "FieldEn": "last price" }, { "FieldName": "currentPrice", "LookupID": 2, "FieldAr": "السعر الحالي", "FieldEn": "current price" } ]; var fieldAr = {}; for(var i=0;i<input.length;i++){ var item = input[i]; fieldAr[item.FieldName] = item.FieldAr; } console.log(fieldAr)
Updated Response 更新的回应
Ok, so you want to accomplish something similar in C# as to the solution posted below. 好的,因此您想在C#中完成与下面发布的解决方案类似的操作。 The first thing you would need to do is deserialize the JSON into an object.
您需要做的第一件事是将JSON反序列化为一个对象。 The easiest way would be to use JSON.NET and convert to a dynamic object and then convert it to a dictionary type.
最简单的方法是使用JSON.NET并将其转换为动态对象 ,然后将其转换为字典类型。 You would return the dictionary as JSON (either using MVC's JSON method or by converting back using JSON.NET)
您将以JSON形式返回字典(使用MVC的JSON方法或使用JSON.NET转换回来)
var json = GetJson();
dynamic fields = JArray.Parse(json);
var myObj = new Dictionary<string, object>();
foreach (var field in fields)
{
myObj.Add((string)field["FieldName"], field["FieldAr"]);
}
Console.WriteLine(JsonConvert.SerializeObject(myObj));
Original Response If I understand correctly, you want the JSON being returned into a javascript object that looks similar to the format: 原始响应如果我理解正确,则希望将JSON返回到看起来与以下格式相似的javascript对象中:
var newObj = {
lastPrice: "اخر سعر",
currentPrice: "السعر الحالي"
};
If this is correct, I would parse the object into a JSON object then create a new object from it. 如果正确的话,我将对象解析为JSON对象,然后从中创建一个新对象。 For example,
例如,
var fields= JSON.parse(data);
var myObj = {};
for(var index = 0; index < fields.length; fields++){
myObj[fields[index].FieldName] = fields[index].FieldAr;
}
It's pretty straight-forward, just loop through each entry and store the property value based on the property name. 这非常简单,只需遍历每个条目并根据属性名称存储属性值。
// Assume you called JSON.parse(json) to get the following parsed data... var parsedData = [{ "FieldName": "lastPrice", "LookupID": 1, "FieldAr": "اخر سعر", "FieldEn": "last price" }, { "FieldName": "currentPrice", "LookupID": 2, "FieldAr": "السعر الحالي", "FieldEn": "current price" }]; var strings = { en: {}, ar: {} }; for(var i = 0; i<parsedData.length;i++){ var data = parsedData[i]; strings.en[data.FieldName] = data.FieldEn; strings.ar[data.FieldName] = data.FieldAr; } console.log(strings);
That will allow you to access properties based on locale and field name, like so: 这样,您就可以根据语言环境和字段名称访问属性,如下所示:
console.log(strings.en.lastPrice); // "last price"
console.log(strings.ar.lastPrice); // "اخر سعر"
You can create a class in c# for your object, instantiate 2 instances of the class with your required property values, add to a list, then use the Newtonsoft.Json library to serialize the list. 您可以在c#中为您的对象创建一个类,使用所需的属性值实例化该类的2个实例,添加到列表中,然后使用Newtonsoft.Json库对列表进行序列化。 Simples :)
简单:)
public class Field{
public string FieldName {get;set;}
public int LookupID {get;set;}
public string FieldAr {get;set;}
public string FieldEn {get;set;}
}
var field1 = new Field(){ FieldName = "lastPrice", LookupID = 1, FieldAr = "اخر سعر", FieldEn = "last price" };
//init other field here
var fields = new List<Field>() {field1};
var fieldsString = JsonConvert.SerializeObject(fields);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.