0

So i have the following Json file:

["burger" : [{"Name": "Chicken Burger", "Price": 3.00},{"Name": "Cheeseburger", "Price": 2.00},{"Name": "beef burger", "Price": 2.00}]"burgerToppings" : [{"Name": "Lettuce"},{"Name": "Tomato"},{"Name":"Cucumber"},]"burgerSauces" : [{"Name": "Mayo"},{"Name": "ketchup"},{"Name": "Spicey mayo"}]]

I would like to store the "Burger" Names and price in a separate ArrayList.

A follow-up question is I would like to be able to append new values/elements into the JSONArray

Update: This is in java, eclipse

3
  • Why not do this with Jackson? Create the entities you need and let Jackson do the heavy lifting Commented Jul 27, 2018 at 20:11
  • I have been trying to use Json simple but no tutorial online and no luck myself, il try Jackson, any links/sources you reccomend? Thanks Commented Jul 27, 2018 at 20:17
  • 1
    You have to flush out the json. currently it's not valid Commented Jul 27, 2018 at 20:31

2 Answers 2

1

You'll have to play around with the schema to fix some properties that are missing like burgerSausce, etc. You get the idea though

Looks like you have malformed JSON. I believe what you're looking for is:

    {"burger" : 

  { "burgerTypes" :
   [
     {"Name": "Chicken Burger", "Price": 3.00},
     {"Name": "Cheeseburger", "Price": 2.00},
     {"Name": "beef burger", "Price": 2.00}
   ]
   },

   "burgerToppings" : [
      {"Name": "Lettuce"},
      {"Name": "Tomato"},
      {"Name":"Cucumber"}
    ],
    "burgerSauces" : [  
      {"Name": "Mayo"},
      {"Name": "ketchup"},
      {"Name": "Spicey mayo"}
    ]  
}

I'd take that JSON, turn it into a schema, and run it through a converter to create some POJO's. Once you have the POJO's generated, use Jackson to do the work for you.

I went to http://www.jsonschema2pojo.org/ and generated the following:

    -----------------------------------com.example.Burger.java-----------------------------------

package com.example;

import java.util.List;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;

@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonPropertyOrder({
"burgerTypes"
})
public class Burger {

@JsonProperty("burgerTypes")
public List<BurgerType> burgerTypes = null;

}
-----------------------------------com.example.BurgerSauce.java-----------------------------------

package com.example;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;

@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonPropertyOrder({
"Name"
})
public class BurgerSauce {

@JsonProperty("Name")
public String name;

}
-----------------------------------com.example.BurgerTopping.java-----------------------------------

package com.example;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;

@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonPropertyOrder({
"Name"
})
public class BurgerTopping {

@JsonProperty("Name")
public String name;

}
-----------------------------------com.example.BurgerType.java-----------------------------------

package com.example;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;

@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonPropertyOrder({
"Name",
"Price"
})
public class BurgerType {

@JsonProperty("Name")
public String name;
@JsonProperty("Price")
public Float price;

}
-----------------------------------com.example.Example.java-----------------------------------

package com.example;

import java.util.List;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;

@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonPropertyOrder({
"burger",
"burgerToppings",
"burgerSauces"
})
public class Example {

@JsonProperty("burger")
public Burger burger;
@JsonProperty("burgerToppings")
public List<BurgerTopping> burgerToppings = null;
@JsonProperty("burgerSauces")
public List<BurgerSauce> burgerSauces = null;

}

Now that you have your entities you can simply call:

Burger burger = objectMapper.readValue(json, Burger.class); 
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks! il try this tomorrow! but pretty much that's all the code I need then? Thank you very much
I hope you'll accept this answer once its all settled
would you be able to add an element to a json array/hashmap? This is one of the issues I was having. Thanks for the all the help!
Yes, but then you would need to regenerate your pojo's. Thats why I said you're going to need to play around with it until you figure out what format you're going to use/ receive.
1

You can use JsonPath to do this easily (once you fix the JSON formatting)

[{"burger" : [{"Name": "Chicken Burger", "Price": 3.00},{"Name": "Cheeseburger", "Price": 2.00},{"Name": "beef burger", "Price": 2.00}],"burgerToppings" : [{"Name": "Lettuce"},{"Name": "Tomato"},{"Name":"Cucumber"}],"burgerSauces" : [{"Name": "Mayo"},{"Name": "ketchup"},{"Name": "Spicey mayo"}]}]

$[0].burger..Name

[
   "Chicken Burger",
   "Cheeseburger",
   "beef burger"
]

And

$[0].burger..Price

[
   3.0,
   2.0,
   2.0
]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.