0

Здравствуйте! Имеется файл objectT.txt содержащий JSON строку:

{
"noData":false,
"activity_empty":"No Data",
"one":[
    {"Id":"111222", "image_visible":true, "image":"ic_new.png", "tv_Id":"123456", "tv_pay":"2,700",     "tv_Date":"jan 26 4:15 PM", "tv_deliveryDate":"jan 30 4:15 PM", "tv_source":"Edwardskkk,

IL", "tv_destination":"Buckeye, AZ",    "activity_weight":"Weight", "tv_weight":"4000", "activity_miles":"Miles", "tv_miles":"1234"},
    {"Id":"222222", "image_visible":false, "image":"ic_new.png", "tv_Id":"123456", "tv_pay":"2,700",    "tv_Date":"jan 26 4:15 PM", "tv_deliveryDate":"jan 30 4:15 PM", "tv_source":"Edwardskkk,

IL", "tv_destination":"Buckeye, AZ",    "activity_weight":"Weight", "tv_weight":"4000", "activity_miles":"Miles", "tv_miles":"1234"}    
     ]
}

затем эту строку переводим в объект objectT, как добавить НОВОЕ поле в объекте objectT, как добавить НОВОЕ поле в массив one объекта objectT (objectT.one), может не нужно переводить строку в объект, а производить манипуляции с ней?

           jQuery.get('objectT.txt', {} ,function(obj)
            {
            objectT = obj;
            objectT = JSON.parse(objectT); 
            //Добавление к objectT нового поля   
            //Добавление к objectT.one нового поля               
            var myJSONText = JSON.stringify(objectT);                
            jQuery('#id-container').append( myJSONText );
            }, 'text')

Что бы получилось следующее:

 {
    "noData":false,
    "activity_empty":"No Data",
    "NEW_FIELD":"NEW_FIELD",
    "NEW_FIELD":"NEW_FIELD",
    "one":[
        {"NEW_FIELD":"NEW_FIELD", "Id":"111222", "image_visible":true, "image":"ic_new.png", "tv_Id":"123456", "tv_pay":"2,700",     "tv_Date":"jan 26 4:15 PM", "tv_deliveryDate":"jan 30 4:15 PM", "tv_source":"Edwardskkk,

    IL", "tv_destination":"Buckeye, AZ",    "activity_weight":"Weight", "tv_weight":"4000", "activity_miles":"Miles", "tv_miles":"1234"},
        {"NEW_FIELD":"NEW_FIELD", "Id":"222222", "image_visible":false, "image":"ic_new.png", "tv_Id":"123456", "tv_pay":"2,700",    "tv_Date":"jan 26 4:15 PM", "tv_deliveryDate":"jan 30 4:15 PM", "tv_source":"Edwardskkk,

    IL", "tv_destination":"Buckeye, AZ",    "activity_weight":"Weight", "tv_weight":"4000", "activity_miles":"Miles", "tv_miles":"1234"}    
         ]
    }

1 ответ 1

5

Можно так:

objectT.NEW_FIELD = 'NEW_FIELD';

или так:

objectT['NEW_FIELD'] = 'NEW_FIELD';
0

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.