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?

1

2 Answers 2

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);
Sign up to request clarification or add additional context in comments.

4 Comments

But this JSONData is coming from URL And how can i display this??
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.
var json=xmlhttp.responseText; obj=jQuery.parseJSON(json); How to print after getting response from AJAX?
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).
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));

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.