GithubHelp home page GithubHelp logo

jsonize's Introduction

jsonize

Convert HTML to JSON. Currently available for .NET Framework 4.6, .NET Standard Library 1.6, and .NET Core 1.0. May be compatible with lower versions, any help porting backwards would be great.

Try it out: http://jsonize.co

Get the NuGet package: https://www.nuget.org/packages/JackWFinlay.Jsonize/

Usage

Use the using statement:

using JackWFinlay.Jsonize;

An example to get the site "http://jackfinlay.com" as a JSON string:

private static async Task<string> Testy(string q = "")
{
    using (var client = new HttpClient())
    {
        string url = @"http://jackfinlay.com";

        HttpResponseMessage response = await client.GetAsync(url);

        string html = await response.Content.ReadAsStringAsync();
        Jsonize jsonize = new Jsonize(html);

        return jsonize.ParseHtmlAsJsonString();
    }
}

Alternatively, get the response as a Newtonsoft.Json JObject:

return jsonize.ParseHtmlAsJson();

Results are in the form:

{
    "node":"Node type e.g. Document, Element, or Comment",
    "tag":"If node is Element this will display the tag e.g p, h1 ,div etc.",
    "text":"If node is of type Text, this will display the text in that node.",
    "attr":{
                "name":"value",
                "name":"value",
                "class": []
            },
    "child":[
                {
                    "node":"Node type e.g. Document, Element, or Comment",
                    "tag":"If node is Element this will display the tag e.g p, h1 ,div etc.",
                    "text":"If node is of type Text, this will display the text in that node.",
                    "child": []
                },
                ...
            ]

Example:

<!DOCTYPE html>
<html>
    <head>
        <title>Jsonize</title>
    </head>
    <body>
        <div id="parent" class="parent-div">
            <div id="child1" class="child-div child1">Some Text</div>
            <div id="child2" class="child-div child2">Some Text</div>
            <div id="child3" class="child-div child3">Some Text</div>
        </div>
    </body>
</html>

Becomes:

{
  "node": "Document",
  "child": [
    {
      "node": "Comment",
      "text": "<!DOCTYPE html>"
    },
    {
      "node": "Element",
      "tag": "html",
      "child": [
        {
          "node": "Element",
          "tag": "head",
          "child": [
            {
              "node": "Element",
              "tag": "title",
              "child": [
                {
                  "node": "Text",
                  "text": "Jsonize"
                }
              ]
            }
          ]
        },
        {
          "node": "Element",
          "tag": "body",
          "child": [
            {
              "node": "Element",
              "tag": "div",
              "attr": {
                "id": "parent",
                "class": [
                  "parent-div"
                ]
              },
              "child": [
                {
                  "node": "Element",
                  "tag": "div",
                  "attr": {
                    "id": "child1",
                    "class": [
                      "child-div",
                      "child1"
                    ]
                  },
                  "child": [
                    {
                      "node": "Text",
                      "text": "Some Text"
                    }
                  ]
                },
                {
                  "node": "Element",
                  "tag": "div",
                  "attr": {
                    "id": "child2",
                    "class": [
                      "child-div",
                      "child2"
                    ]
                  },
                  "child": [
                    {
                      "node": "Text",
                      "text": "Some Text"
                    }
                  ]
                },
                {
                  "node": "Element",
                  "tag": "div",
                  "attr": {
                    "id": "child3",
                    "class": [
                      "child-div",
                      "child3"
                    ]
                  },
                  "child": [
                    {
                      "node": "Text",
                      "text": "Some Text"
                    }
                  ]
                }
              ]
            }
          ]
        }
      ]
    }
  ]
}

TODO:

  • Port to .Net 4.6.
  • Add support to directly pass in a URL.
  • Remove formatting issues such as empty child arrays on Script tags with no content. Fixed in 1.0.3
  • Add Documentation.
  • Change class attribute to array rather than space separated. Fixed in 1.0.2

jsonize's People

Contributors

jackwfinlay avatar

Watchers

James Cloos avatar Brandon Everett avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.