0

I have this JSON Data coming from some URL: I have to parse this JSON Data and Print all the products brand name, MRP and name.

 [
        {
            "name": "Message",
            "data": []
        },
        {
            "name": "ProductGrid",
            "data": {
                "title": "Results for tea",
                "count": 244,
                "products": [
                    {
                        "id": "2313",
                        "name": "Clear Green Tea Bags",
                        "full_name": "Lipton Clear Green Tea Bags",
                        "images": [
                            "200.png"
                        ],
                        "brand": {
                            "id": "18",
                            "name": "Lipton",
                            "url": "/lipton-b.php"
                        },
                        "category": {
                            "id": "86",
                            "name": "Tea Bag",
                            "url": "tea-bag-c.php",
                            "food_coupons_allowed": "1",
                            "image_url": "hea-bag.jpeg",
                            "parent_category": {
                                "id": "13",
                                "name": "Tea & Coffee"
                            }
                        },
                        "properties": [],
                        "is_new": false,
                        "     

]'

I want to parse this JSON data to an array using JavaScript or Java And I need full_name,brand & MRP of products.

How to do it please help?

Adnan
  • 1,440
  • 2
  • 18
  • 38
  • 1
    This json is not well formatted, anyway in 10 seconds you can find plenty of resources on stackoverflow, like this: http://stackoverflow.com/questions/1688099/converting-json-to-java – Paolof76 Sep 23 '15 at 08:43

2 Answers2

1

In javascript you can do that:

var jsonText = '[{"name":"Message", "data": [] ... // jsonText is the string with the json 
var obj = JSON.parse(jsonText);

In java you can represent your json as a Map.

String json = ...
ObjectMapper mapper = new ObjectMapper();
Map<String,Object> map = mapper.readValue(json, Map.class);
Davide Lorenzo MARINO
  • 26,420
  • 4
  • 39
  • 56
  • But this JSONData is coming from URL And how can i display this?? – Adnan Sep 23 '15 at 08:56
  • You have to convert it to a string. If it comes from url, from the body of an ajax call or just from a string in your code it doesn't change. – Davide Lorenzo MARINO Sep 23 '15 at 09:30
  • **var json=xmlhttp.responseText; obj=jQuery.parseJSON(json);** How to print after getting response from AJAX? – Adnan Sep 23 '15 at 09:34
  • Now it is an object. You can place a breakpoint on your javascript code and inspect it with the integrated debugger on your browser (or printing it with console.log function). – Davide Lorenzo MARINO Sep 23 '15 at 09:38
0

suppose you have URL like var url = http://localhost?jsonData=[...]

In javascript, u can get jsonData like var jsonData = url.split("=")[1];

But it is of string type. To parse it to JSON write jsonData = JSON.parse(jsonData)

Now to get an array of Strings which you want to display, var printData = jsonData.map(function(jsonObj){ return jsonobj.title + jsonobj.brand.name + jsonObj.price; })

Now you can see printData in console using below code console.log(JSON.stringify(printData));

krishna
  • 154
  • 7