Loading ...
Sorry, an error occurred while loading the content.

738Re: [json] Re: JSON with HTML snippet

Expand Messages
  • shalini israni
    Feb 14, 2007
      I donno...sometimes using APIs is messy... i used a JavaAPI onec..that was
      great...but this one time it really messed me up bad...so i had to resort to
      creating my own JSON object on the server side... pasted the code below....
      mine to was XHTML that caused the API to trip....

      This is Java code..but maybe u can look at it and write the PHP equivalent
      .....


      public JSONObject feedToJSONObject(String feedContent) {
      JSONObject json = new JSONObject(); // main json object to be
      returned
      JSONArray itemArray = new JSONArray();
      Document doc = XMLEditor.getDOMFromXMLString(feedContent);

      NodeList items = doc.getElementsByTagName("item"); // items should
      be a
      // JSONArray
      for (int i = 0; i < items.getLength(); i++) {
      NodeList children = items.item(i).getChildNodes();
      String data = "";
      for (int j = 0; j < children.getLength(); j++) { // this part
      // relies on the
      // fact that
      // title comes
      // before link
      // hence the
      // comma
      if (children.item(j).getNodeName().equalsIgnoreCase("link"))
      data += "\"link\":\"" + children.item
      (j).getFirstChild().getNodeValue()
      + "\"}";
      if (children.item
      (j).getNodeName().equalsIgnoreCase("title"))
      data += "{\"title\":\"" + children.item
      (j).getFirstChild().getNodeValue()
      + "\",";
      }
      try {
      JSONObject itemChildren = new JSONObject(data);
      itemArray.put(itemChildren);
      } catch (JSONException jEx) {
      jEx.printStackTrace();
      }
      }
      try {
      json.put("items", itemArray);
      } catch (JSONException jEx) {
      jEx.printStackTrace();
      }
      return json;

      }


      On 2/15/07, weilandaj <weilandaj@...> wrote:
      >
      > I'm using the php json extension.
      >
      > http://us3.php.net/manual/en/function.json-encode.php
      >
      > It generates exactly what I pasted in my first message. After it gets
      > eval()'ed it gets mangled. I did some more testing and one thing that
      > stood out is "href" seems to get yanked out for some reason in the
      > eval. Not sure if that's some kind of security thing
      >
      > --- In json@yahoogroups.com <json%40yahoogroups.com>, "shalini israni" <
      > shaliny.i@...> wrote:
      > >
      > > How are you constructing the JSON object on the serverside before u
      > send it
      > > to the client side? I feel something is mucking up there... Could u
      > paste
      > > that code??? I'm not into php...but maybe someone else on the group
      > is... :)
      > > No sweat..it'll get solved!
      > >
      > > On 2/14/07, weilandaj <weilandaj@...> wrote:
      > > >
      > > > Hi All,
      > > >
      > > > I'm having some trouble with an element of my web application with
      > json.
      > > >
      > > > The short story is I am sending database as part of an ajax request.
      > > > One piece of that data is HTML text. On the server end I'm using php
      > > > with smarty templates and need to send a short bit of HTML that comes
      > > > from a template.
      > > >
      > > > The problem is the html snippet is being severely mangled on the
      > > > browser end (tested in IE and FF). Here's an example:
      > > >
      > > > {"html":"<a href=\"4427-New-Gaming-Channel.html\"\>New Gaming
      > > > Channel</a\><b\> . 8 Views</b\>","id":"4427"}
      > > >
      > > > The HTML becomes:
      > > >
      > > > <a ew-Gaming-Channel.html">New Gaming Channel</a><b> . 8 Views</b>
      > > >
      > > > I've tried all sorts of escaping and other things. I'm really stuck
      > > > as to why this happens.
      > > >
      > > > Thanks for you help.
      > > >
      > > >
      > > >
      > >
      > >
      > >
      > > --
      > >
      > > Shalini
      > >
      > >
      > > [Non-text portions of this message have been removed]
      > >
      >
      >
      >



      --

      Shalini


      [Non-text portions of this message have been removed]
    • Show all 7 messages in this topic