Foros del Web » Programando para Internet » Javascript »

Decode Json

Estas en el tema de Decode Json en el foro de Javascript en Foros del Web. Hola migo acá necesito un ayuda con Json, bueno para Json soy algo novato pero voy aprendiendo poco a poco así que mi duda es ...
  #1 (permalink)  
Antiguo 05/12/2015, 09:29
 
Fecha de Ingreso: marzo-2011
Ubicación: Punta de Mata
Mensajes: 106
Antigüedad: 13 años, 1 mes
Puntos: 0
Decode Json

Hola migo acá necesito un ayuda con Json, bueno para Json soy algo novato pero voy aprendiendo poco a poco así que mi duda es con un decode de Json a partir de javascript, ya que en php ya le resulto, vean

Código HTML:
Ver original
  1. \u003Cdiv\u003EHola\u003E\u003C\/div\u003E

esto es encode de un simple div por php y lo que quiero es que cuando sea solicitado por script le envié la consulta a un php y de resulta sea lo que mostré y que el script decode ese archivo pero se me ha hecho complicado. Lo que he buscado por internet ese este script

Código Javascript:
Ver original
  1. function json_decode(str_json) {
  2.   //       discuss at: http://phpjs.org/functions/json_decode/
  3.   //      original by: Public Domain (http://www.json.org/json2.js)
  4.   // reimplemented by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
  5.   //      improved by: T.J. Leahy
  6.   //      improved by: Michael White
  7.   //        example 1: json_decode('[ 1 ]');
  8.   //        returns 1: [1]
  9.  
  10.   /*
  11.        http://www.JSON.org/json2.js
  12.        2008-11-19
  13.        Public Domain.
  14.        NO WARRANTY EXPRESSED OR IMPLIED. USE AT YOUR OWN RISK.
  15.        See http://www.JSON.org/js.html
  16.      */
  17.  
  18.   var json = this.window.JSON;
  19.   if (typeof json === 'object' && typeof json.parse === 'function') {
  20.     try {
  21.       return json.parse(str_json);
  22.     } catch (err) {
  23.       if (!(err instanceof SyntaxError)) {
  24.         throw new Error('Unexpected error type in json_decode()');
  25.       }
  26.       this.php_js = this.php_js || {};
  27.       // usable by json_last_error()
  28.       this.php_js.last_error_json = 4;
  29.       return null;
  30.     }
  31.   }
  32.  
  33.   var cx = /[\u0000\u00ad\u0600-\u0604\u070f\u17b4\u17b5\u200c-\u200f\u2028-\u202f\u2060-\u206f\ufeff\ufff0-\uffff]/g;
  34.   var j;
  35.   var text = str_json;
  36.  
  37.   // Parsing happens in four stages. In the first stage, we replace certain
  38.   // Unicode characters with escape sequences. JavaScript handles many characters
  39.   // incorrectly, either silently deleting them, or treating them as line endings.
  40.   cx.lastIndex = 0;
  41.   if (cx.test(text)) {
  42.     text = text.replace(cx, function(a) {
  43.       return '\\u' + ('0000' + a.charCodeAt(0)
  44.           .toString(16))
  45.         .slice(-4);
  46.     });
  47.   }
  48.  
  49.   // In the second stage, we run the text against regular expressions that look
  50.   // for non-JSON patterns. We are especially concerned with '()' and 'new'
  51.   // because they can cause invocation, and '=' because it can cause mutation.
  52.   // But just to be safe, we want to reject all unexpected forms.
  53.   // We split the second stage into 4 regexp operations in order to work around
  54.   // crippling inefficiencies in IE's and Safari's regexp engines. First we
  55.   // replace the JSON backslash pairs with '@' (a non-JSON character). Second, we
  56.   // replace all simple value tokens with ']' characters. Third, we delete all
  57.   // open brackets that follow a colon or comma or that begin the text. Finally,
  58.   // we look to see that the remaining characters are only whitespace or ']' or
  59.   // ',' or ':' or '{' or '}'. If that is so, then the text is safe for eval.
  60.   if ((/^[\],:{}\s]*$/)
  61.     .test(text.replace(/\\(?:["\\\/bfnrt]|u[0-9a-fA-F]{4})/g, '@')
  62.       .replace(/"[^"\\\n\r]*"|true|false|null|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?/g, ']')
  63.       .replace(/(?:^|:|,)(?:\s*\[)+/g, ''))) {
  64.  
  65.     // In the third stage we use the eval function to compile the text into a
  66.     // JavaScript structure. The '{' operator is subject to a syntactic ambiguity
  67.     // in JavaScript: it can begin a block or an object literal. We wrap the text
  68.     // in parens to eliminate the ambiguity.
  69.     j = eval('(' + text + ')');
  70.  
  71.     return j;
  72.   }
  73.  
  74.   this.php_js = this.php_js || {};
  75.   // usable by json_last_error()
  76.   this.php_js.last_error_json = 4;
  77.   return null;
  78. }

pero aun no se dominarlo
por favor si hay alguien que me ayuda, se lo agradezco ante amno.
  #2 (permalink)  
Antiguo 08/12/2015, 15:25
 
Fecha de Ingreso: diciembre-2015
Mensajes: 67
Antigüedad: 8 años, 4 meses
Puntos: 2
Respuesta: Decode Json

No uses ese script intenta con esto:

Código Javascript:
Ver original
  1. var RESPUESTA_JSON = JSON.decode(JSON);

La función JSON.decode transforma cualquier texto JSON en una variable, array u objeto JS.

Etiquetas: json, php, scripts
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 12:27.