How to add two JSON formatted Java Strings together? - java

I have two JSON Format Strings
{"user1":{"Iden":4,"nID":1},"user2":{"Iden":5,"nID":1}} // String A JSON
{"user1":{"Iden":4,"nID":1},"user3":{"Iden":6,"nID":1},"user2":{"Iden":5,"nID":1}}
In the below program these above JSON are formatted by Eclipse IDE
This is my program:
import java.util.Map;
import org.codehaus.jackson.type.TypeReference;
import com.tradeking.at.util.JsonHelper;
public class Hi {
private static JsonHelper jsonHelper = JsonHelper.getInstance();
public static void main(String[] args) throws Exception {
Map<String, Tracker> totalCusts = null;
String A = "{\"user1\":{\"Iden\":4,\"nID\":1},\"user2\":{\"Iden\":5,\"nID\":1}}";
String B = "{\"user1\":{\"Iden\":4,\"nID\":1},\"user3\":{\"Iden\":6,\"nID\":1},\"user2\":{\"Iden\":5,\"nID\":1}}";
String totalString = A+B;
if (null != totalString) {
totalCusts = (Map<String, Tracker>) jsonHelper.toObject(
totalString, new TypeReference<Map<String, Tracker>>() {
});
}
System.out.println(totalCusts);
}
}
Tracker.java:
import org.json.JSONObject;
public class Tracker extends JSONObject{
}
When i ran the above , the Output is
{user1={}, user2={}}
if I use this:
String totalString = B + A ;
The O/p is:
{user1={}, user3={}, user2={}}
Please let me know how I can add two JSON Strings??

At the top-level, a JSON document is always a single object, array, or value. By just concatenating the two strings together, you're violating this principal. A simple workaround would be to join the two values together in an array:
String totalString = "[" + A + ", " + B + "]";
And then parse as such. Or you could simply parse each JSON document one at a time, and then append or merge your results (I suspect you want to merge them, via Map.putAll).
Given that the values for your userN keys are empty, you probably have a bug in your JsonHelper class, but that's hard to say without seeing the code.

Related

Split JSON objects in array string using regex

I have a String in the following format:
[{"HostName":"taskmanager1","Rack":"/default-rack","State":"RUNNING","NodeId":"taskmanager1:45454","NodeHTTPAddress":"taskmanager1:8042","LastHealthUpdate":1519568501615,"HealthReport":"","NodeManagerVersion":"2.8.3","NumContainers":0,"UsedMemoryMB":0,"AvailableMemoryMB":1024},{"HostName":"datanode2","Rack":"/default-rack","State":"RUNNING","NodeId":"datanode2:45454","NodeHTTPAddress":"datanode2:8042","LastHealthUpdate":1519260876106,"HealthReport":"","NodeManagerVersion":"2.8.3","NumContainers":0,"UsedMemoryMB":0,"AvailableMemoryMB":1024},{"HostName":"taskmanager3","Rack":"/default-rack","State":"RUNNING","NodeId":"taskmanager3:45454","NodeHTTPAddress":"taskmanager3:8042","LastHealthUpdate":1519568502251,"HealthReport":"","NodeManagerVersion":"2.8.3","NumContainers":0,"UsedMemoryMB":0,"AvailableMemoryMB":1024},{"HostName":"datanode3","Rack":"/default-rack","State":"RUNNING","NodeId":"datanode3:45454","NodeHTTPAddress":"datanode3:8042","LastHealthUpdate":1519260871527,"HealthReport":"","NodeManagerVersion":"2.8.3","NumContainers":0,"UsedMemoryMB":0,"AvailableMemoryMB":1024},{"HostName":"taskmanager2","Rack":"/default-rack","State":"RUNNING","NodeId":"taskmanager2:45454","NodeHTTPAddress":"taskmanager2:8042","LastHealthUpdate":1519568502259,"HealthReport":"","NodeManagerVersion":"2.8.3","NumContainers":0,"UsedMemoryMB":0,"AvailableMemoryMB":1024},{"HostName":"datanode1","Rack":"/default-rack","State":"RUNNING","NodeId":"datanode1:45454","NodeHTTPAddress":"datanode1:8042","LastHealthUpdate":1519260875647,"HealthReport":"","NodeManagerVersion":"2.8.3","NumContainers":0,"UsedMemoryMB":0,"AvailableMemoryMB":1024}]
I want to split it into multiple (here 6) JSON format, but my pattern cannot split that as desired.
I want something like this:
{"HostName":"taskmanager1","Rack":"/default-rack","State":"RUNNING","NodeId":"taskmanager1:45454","NodeHTTPAddress":"taskmanager1:8042","LastHealthUpdate":1519568501615,"HealthReport":"","NodeManagerVersion":"2.8.3","NumContainers":0,"UsedMemoryMB":0,"AvailableMemoryMB":1024},
{"HostName":"datanode2","Rack":"/default-rack","State":"RUNNING","NodeId":"datanode2:45454","NodeHTTPAddress":"datanode2:8042","LastHealthUpdate":1519260876106,"HealthReport":"","NodeManagerVersion":"2.8.3","NumContainers":0,"UsedMemoryMB":0,"AvailableMemoryMB":1024},
{"HostName":"taskmanager3","Rack":"/default-rack","State":"RUNNING","NodeId":"taskmanager3:45454","NodeHTTPAddress":"taskmanager3:8042","LastHealthUpdate":1519568502251,"HealthReport":"","NodeManagerVersion":"2.8.3","NumContainers":0,"UsedMemoryMB":0,"AvailableMemoryMB":1024},
{"HostName":"datanode3","Rack":"/default-rack","State":"RUNNING","NodeId":"datanode3:45454","NodeHTTPAddress":"datanode3:8042","LastHealthUpdate":1519260871527,"HealthReport":"","NodeManagerVersion":"2.8.3","NumContainers":0,"UsedMemoryMB":0,"AvailableMemoryMB":1024}
,{"HostName":"taskmanager2","Rack":"/default-rack","State":"RUNNING","NodeId":"taskmanager2:45454","NodeHTTPAddress":"taskmanager2:8042","LastHealthUpdate":1519568502259,"HealthReport":"","NodeManagerVersion":"2.8.3","NumContainers":0,"UsedMemoryMB":0,"AvailableMemoryMB":1024},
{"HostName":"datanode1","Rack":"/default-rack","State":"RUNNING","NodeId":"datanode1:45454","NodeHTTPAddress":"datanode1:8042","LastHealthUpdate":1519260875647,"HealthReport":"","NodeManagerVersion":"2.8.3","NumContainers":0,"UsedMemoryMB":0,"AvailableMemoryMB":1024}
Using the code:
List<String> res = Arrays.asList(temp.replace('[', ' ').replace(']',' ').trim()).split(",");
It will be split for every , character and using the pattern split("},\\}") will remove } and { character, too.
How can I split that as desire to make Json objects?
Using the Java pattern (\\{.+}) will group whole string.
You can parse the JSON as an array and treat the contents as individual strings. Here is sample code:
import org.json.JSONArray;
public class orgJson1Main {
private static final String sample = "[{\"HostName\":\"taskmanager1\",\"Rack\":\"/default-rack\",\"State\":\"RUNNING\",\"NodeId\":\"taskmanager1:45454\",\"NodeHTTPAddress\":\"taskmanager1:8042\",\"LastHealthUpdate\":1519568501615,\"HealthReport\":\"\",\"NodeManagerVersion\":\"2.8.3\",\"NumContainers\":0,\"UsedMemoryMB\":0,\"AvailableMemoryMB\":1024},{\"HostName\":\"datanode2\",\"Rack\":\"/default-rack\",\"State\":\"RUNNING\",\"NodeId\":\"datanode2:45454\",\"NodeHTTPAddress\":\"datanode2:8042\",\"LastHealthUpdate\":1519260876106,\"HealthReport\":\"\",\"NodeManagerVersion\":\"2.8.3\",\"NumContainers\":0,\"UsedMemoryMB\":0,\"AvailableMemoryMB\":1024},{\"HostName\":\"taskmanager3\",\"Rack\":\"/default-rack\",\"State\":\"RUNNING\",\"NodeId\":\"taskmanager3:45454\",\"NodeHTTPAddress\":\"taskmanager3:8042\",\"LastHealthUpdate\":1519568502251,\"HealthReport\":\"\",\"NodeManagerVersion\":\"2.8.3\",\"NumContainers\":0,\"UsedMemoryMB\":0,\"AvailableMemoryMB\":1024},{\"HostName\":\"datanode3\",\"Rack\":\"/default-rack\",\"State\":\"RUNNING\",\"NodeId\":\"datanode3:45454\",\"NodeHTTPAddress\":\"datanode3:8042\",\"LastHealthUpdate\":1519260871527,\"HealthReport\":\"\",\"NodeManagerVersion\":\"2.8.3\",\"NumContainers\":0,\"UsedMemoryMB\":0,\"AvailableMemoryMB\":1024},{\"HostName\":\"taskmanager2\",\"Rack\":\"/default-rack\",\"State\":\"RUNNING\",\"NodeId\":\"taskmanager2:45454\",\"NodeHTTPAddress\":\"taskmanager2:8042\",\"LastHealthUpdate\":1519568502259,\"HealthReport\":\"\",\"NodeManagerVersion\":\"2.8.3\",\"NumContainers\":0,\"UsedMemoryMB\":0,\"AvailableMemoryMB\":1024},{\"HostName\":\"datanode1\",\"Rack\":\"/default-rack\",\"State\":\"RUNNING\",\"NodeId\":\"datanode1:45454\",\"NodeHTTPAddress\":\"datanode1:8042\",\"LastHealthUpdate\":1519260875647,\"HealthReport\":\"\",\"NodeManagerVersion\":\"2.8.3\",\"NumContainers\":0,\"UsedMemoryMB\":0,\"AvailableMemoryMB\":1024}]";
public static void main(String[] args) {
JSONArray array = new JSONArray(sample);
for(int i=0; i < array.length(); i++){
System.out.println(array.get(i));
}
}
}
OUTPUT:
{"NodeManagerVersion":"2.8.3","Rack":"/default-rack","LastHealthUpdate":1519568501615,"HealthReport":"","State":"RUNNING","AvailableMemoryMB":1024,"NodeId":"taskmanager1:45454","UsedMemoryMB":0,"NodeHTTPAddress":"taskmanager1:8042","HostName":"taskmanager1","NumContainers":0}
{"NodeManagerVersion":"2.8.3","Rack":"/default-rack","LastHealthUpdate":1519260876106,"HealthReport":"","State":"RUNNING","AvailableMemoryMB":1024,"NodeId":"datanode2:45454","UsedMemoryMB":0,"NodeHTTPAddress":"datanode2:8042","HostName":"datanode2","NumContainers":0}
{"NodeManagerVersion":"2.8.3","Rack":"/default-rack","LastHealthUpdate":1519568502251,"HealthReport":"","State":"RUNNING","AvailableMemoryMB":1024,"NodeId":"taskmanager3:45454","UsedMemoryMB":0,"NodeHTTPAddress":"taskmanager3:8042","HostName":"taskmanager3","NumContainers":0}
{"NodeManagerVersion":"2.8.3","Rack":"/default-rack","LastHealthUpdate":1519260871527,"HealthReport":"","State":"RUNNING","AvailableMemoryMB":1024,"NodeId":"datanode3:45454","UsedMemoryMB":0,"NodeHTTPAddress":"datanode3:8042","HostName":"datanode3","NumContainers":0}
{"NodeManagerVersion":"2.8.3","Rack":"/default-rack","LastHealthUpdate":1519568502259,"HealthReport":"","State":"RUNNING","AvailableMemoryMB":1024,"NodeId":"taskmanager2:45454","UsedMemoryMB":0,"NodeHTTPAddress":"taskmanager2:8042","HostName":"taskmanager2","NumContainers":0}
{"NodeManagerVersion":"2.8.3","Rack":"/default-rack","LastHealthUpdate":1519260875647,"HealthReport":"","State":"RUNNING","AvailableMemoryMB":1024,"NodeId":"datanode1:45454","UsedMemoryMB":0,"NodeHTTPAddress":"datanode1:8042","HostName":"datanode1","NumContainers":0}
EDIT:
First, I removed the JSONTokener from the above code. Second, for completeness I'm adding the following code that shows how to find the individual JSON objects within the sample string using a regex as originally asked.
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class orgJson1Main {
private static final String sample = "[{\"HostName\":\"taskmanager1\",\"Rack\":\"/default-rack\",\"State\":\"RUNNING\",\"NodeId\":\"taskmanager1:45454\",\"NodeHTTPAddress\":\"taskmanager1:8042\",\"LastHealthUpdate\":1519568501615,\"HealthReport\":\"\",\"NodeManagerVersion\":\"2.8.3\",\"NumContainers\":0,\"UsedMemoryMB\":0,\"AvailableMemoryMB\":1024},{\"HostName\":\"datanode2\",\"Rack\":\"/default-rack\",\"State\":\"RUNNING\",\"NodeId\":\"datanode2:45454\",\"NodeHTTPAddress\":\"datanode2:8042\",\"LastHealthUpdate\":1519260876106,\"HealthReport\":\"\",\"NodeManagerVersion\":\"2.8.3\",\"NumContainers\":0,\"UsedMemoryMB\":0,\"AvailableMemoryMB\":1024},{\"HostName\":\"taskmanager3\",\"Rack\":\"/default-rack\",\"State\":\"RUNNING\",\"NodeId\":\"taskmanager3:45454\",\"NodeHTTPAddress\":\"taskmanager3:8042\",\"LastHealthUpdate\":1519568502251,\"HealthReport\":\"\",\"NodeManagerVersion\":\"2.8.3\",\"NumContainers\":0,\"UsedMemoryMB\":0,\"AvailableMemoryMB\":1024},{\"HostName\":\"datanode3\",\"Rack\":\"/default-rack\",\"State\":\"RUNNING\",\"NodeId\":\"datanode3:45454\",\"NodeHTTPAddress\":\"datanode3:8042\",\"LastHealthUpdate\":1519260871527,\"HealthReport\":\"\",\"NodeManagerVersion\":\"2.8.3\",\"NumContainers\":0,\"UsedMemoryMB\":0,\"AvailableMemoryMB\":1024},{\"HostName\":\"taskmanager2\",\"Rack\":\"/default-rack\",\"State\":\"RUNNING\",\"NodeId\":\"taskmanager2:45454\",\"NodeHTTPAddress\":\"taskmanager2:8042\",\"LastHealthUpdate\":1519568502259,\"HealthReport\":\"\",\"NodeManagerVersion\":\"2.8.3\",\"NumContainers\":0,\"UsedMemoryMB\":0,\"AvailableMemoryMB\":1024},{\"HostName\":\"datanode1\",\"Rack\":\"/default-rack\",\"State\":\"RUNNING\",\"NodeId\":\"datanode1:45454\",\"NodeHTTPAddress\":\"datanode1:8042\",\"LastHealthUpdate\":1519260875647,\"HealthReport\":\"\",\"NodeManagerVersion\":\"2.8.3\",\"NumContainers\":0,\"UsedMemoryMB\":0,\"AvailableMemoryMB\":1024}]";
public static void main(String[] args) {
Matcher matcher = Pattern.compile("\\{[^}]*\\}").matcher(sample);
while(matcher.find()){
System.out.println(matcher.group());
}
}
}
To split on }, {, but retain the curly brackets in the tokens, split on this regex:
"(?<=\\}), (?=\\{)"
Which uses a look behind and a look ahead to assert the curly brackets preceed and follow the comma, but not consume them in the split.
The whole line then becomes:
List<String> res = Arrays.asList(temp.replaceAll("^.|.$", "").split("(?<=\\}), (?=\\{)");
Note also the simplified trimming of leading [ and trailing ] but more-simply removing the first and last character in one operation.
If your purpose to use this List as list of MyJsonObject I would recommend to reuse brilliant google gson library.
There is easy way to convert String to List without intermediate manipulation with List.
What you need to follow followed steps.
1) Create your POJO class:
public class POJO
{
String HostName;
String Rack;
String State;
String NodeId;
String NodeHTTPAddress;
String LastHealthUpdate;
String HealthReport;
String NodeManagerVersion;
String NumContainers;
String UsedMemoryMB;
String AvailableMemoryMB;
... getters/setters here ....
}
2) Create gson converter:
Gson gson = (new GsonBuilder() ).create();
3) Create typeToken for list of your POJOs:
Type type = new TypeToken< List<POJO> >(){}.getType();
4) Convert String to desire collection:
List<MyJsonObject> list = gson.fromJson( json, type );

Easy way to get to values from JSONArray

I recently started working with JSON in Java. We have been setting and getting our values as follows from this JSONArray:
[{"productId":"1"},{"productName":"hammer"}]
JSONObject jo = ja.getJSONObject(0);
We could easily get the values by calling jo.getString("productId"); which would return the 1.
The problem is that sometimes we get different types of JSON objects. They look like this:
[{"name":"productId", "value":"1"},{"name":"productName", "value":"hammer"}]
Is there a way to easily eliminate those predicate name/value and just group the actual name and value together (as in the first example)?
The short answer is no.
The longer answer is that you're not working with JSON, you're working with someone's misunderstanding of JSON.
Both of your examples look a bit like JSON, but they're both bogus.
[] is an array.
{} is an object.
Your first string [{"productId":"1"},{"productName":"hammer"}]
is an array of two objects, where each object has one property.
It's confusing to put dissimilar objects into an array together, but that's going on in both of your examples.
The second example [{"name":"productId", "value":"1"},{"name":"productName", "value":"hammer"}] shows an array of two objects, but again, the objects are dissimilar.
I think what they're going for is more like [{"productId":"1","productName":"hammer"}], so I guess the long answer to your question is that you need to go to whomever is providing this "JSON" and tell them to fix it.
To give you a clearer idea of the correspondence between objects (in Java and otherwise) and JSON, check out the Java program below:
public class Product {
String productName;
String productId;
public Product(String productId,String productName){
this.productName = productName;
this.productId = productId;
}
public String toString(){return toJSONString();}
public String toJSONString(){
return "{\"productId\":\""+productId+",\"productName:\""+productName+"\"}";
}
public static String arrayToJSONString(Product[] arry){
StringBuilder sb = new StringBuilder();
sb.append("["+arry[0]);
for (int n =1;n<arry.length;n++){
sb.append(","+arry[n]);
}
sb.append("]");
return sb.toString();
}
public static void main(String [] args){
Product p1 = new Product("1","hammer");
Product[] arry = {p1};
Product[] arry2 ={p1,new Product("2","shovel"), new Product("3","manure")};
System.out.println("One object");
System.out.println(" "+p1);
System.out.println("An array containing one object");
System.out.println(" "+Product.arrayToJSONString(arry));
System.out.println("An array containing three objects");
System.out.println(" "+Product.arrayToJSONString(arry2));
}
}
Here's the output showing the proper JSON representation:
One object
{"productId":"1,"productName:"hammer"}
An array containing one object
[{"productId":"1,"productName:"hammer"}]
An array containing three objects
[{"productId":"1,"productName:"hammer"},{"productId":"2,"productName:"shovel"},{"productId":"3,"productName:"manure"}]
(Newlines are an artifact of the HTML, not JSON)

i have four string values from method. how to combine strings?

so i will get the value of these string from a method, and i need combine them as a file address, but i can't combine the string like i did on FILE_PATH_STRING. I tried to use concat method, but it doesn't work too. FILE_PATH_STRING will always equal to the first string ,which is WORLD_PATH in this case
public static final String WORLD_PATH = "The World/";
public static String CONTINENTS_NAME="";
public static String COUNTRY_NAME="";
public static String FILE_TYPE="";
public static String FILE_PATH_STRING = WORLD_PATH + CONTINENTS_NAME + COUNTRY_NAME + FILE_TYPE;
public static File FILE_PATH = new File(FILE_PATH_STRING);
it should work like, when i click on a map, method will return the name of region to me,and i wills store them in those static string. I tried debug. All of the strings have a value and they correct. but FILE_PATH_STRING only equal to the first string i put in there.
after i run the program,
CONTINENTS_NAME = Asia
COUNTRY_NAME should equal to CONTINENTS_NAME because that's how i set up my file address
FILE_PATH = .png this is method for load map
I am curious, how do you "have four string values from a method" - can you enlighten us on that? Because if it is anything like this:
getStrings(WORLD_PATH, CONTINENTS_NAME, COUNTRY_NAME, FILE_TYPE);
Then that's the problem - the function getStrings() can't modify those strings passed in.
I am only making wild guesses of course, since you haven't given enough information.
Can you do something like:
String path = getWorldPath() + getContinent() + getCountry() + getFileType();
That is, implement four separate methods to get the four separate parts of the path?
Otherwise you will have to define a type which can return all four strings at the same time, or return them in a container like an array:
void test()
{
String[] pathParts = getStrings();
FILE_PATH_STRING = pathParts[0] + pathParts[1] + pathParts[2] + pathParts[3];
}
String[] getStrings()
{
String[] ret = new String[4];
ret[0] = "The world";
ret[1] = "South America";
ret[2] = "Chile";
ret[3] = ".txt";
return ret;
}
Of course, if all you are going to do is concatenate the strings, you could do that in the getStrings() function.

Problem in Parsing JSON in Android

Hello friends i am having problem in parsing jSON web services data
I have the following set of data i got from my web services
[{"store_id":"81","store_name":"Mayo - Castlebar McDrive","store_type":"Drive-Thru",
"vouchers_available":"Vouchers available","store_limit":"10","distance":"8123.33 km",
"latitude":"53.8501090162671","longitude":"-9.29713726043701","image_name":"http:\/\/www.mcfinder.ie\/admin\/images\/stores\/default.png",
"voucher_count":"2","is_open":"Restaurant Open","attributes":[{"attribute_name":"Wi-Fiiiii",
"image_name":"http:\/\/www.mcfinder.ie\/admin\/images\/attributes\/t_wifi_icon.gif"},{"attribute_name":"Cashless",
"image_name":"http:\/\/www.mcfinder.ie\/admin\/images\/attributes\/t_cashless_icon.gif"},
{"attribute_name":"McDrive","image_name":"http:\/\/www.mcfinder.ie\/admin\/images\/attributes\/car_icon.png"}]}]
and i m using this code to parse the DATA but i am getting the error
Please friends i am new in JSON Web services guide me what am i doing wrong.
CODE:
JSONObject jObject = new JSONObject(data);
JSONArray array = jObject.getJSONArray("attributes");
ERROR:
07-19 23:43:02.437: WARN/System.err(674): org.json.JSONException: A JSONObject text must begin with '{' at character 2 of
M waiting for some positive response and guidelines
Thanks
If you're going to work with JSON data, I recommend reviewing the JSON introduction at http://json.org, until it is thoroughly understood. Luckily, JSON is a relatively simple data format, and becoming comfortable with it comes quickly.
To the specific problem in the original question, note that the outer-most structure of the JSON data is an array. So, it needs to be read as an array -- not as an object.
Here's a brief description of the complete JSON structure.
An array with one element that is an unnamed object. The unnamed object has twelve elements, eleven of which are strings, and one of which, named "attributes", is an array of three objects. Each object in the "attributes" array has two string elements.
So, if you want the "attributes" array, first read in the entire contents as an array, then get the first component of the array as an object, then get the "attributes" element from that object as an array. Following is an example of doing this.
import java.math.BigDecimal;
import java.net.URI;
import java.util.List;
import org.json.JSONArray;
import org.json.JSONObject;
public class Foo
{
public static void main(String[] args) throws Exception
{
JSONArray outerArray = new JSONArray("[{\"store_id\":\"81\",\"store_name\":\"Mayo - Castlebar McDrive\",\"store_type\":\"Drive-Thru\",\"vouchers_available\":\"Vouchers available\",\"store_limit\":\"10\",\"distance\":\"8123.33 km\",\"latitude\":\"53.8501090162671\",\"longitude\":\"-9.29713726043701\",\"image_name\":\"http:\\/\\/www.mcfinder.ie\\/admin\\/images\\/stores\\/default.png\",\"voucher_count\":\"2\",\"is_open\":\"Restaurant Open\",\"attributes\":[{\"attribute_name\":\"Wi-Fiiiii\",\"image_name\":\"http:\\/\\/www.mcfinder.ie\\/admin\\/images\\/attributes\\/t_wifi_icon.gif\"},{\"attribute_name\":\"Cashless\",\"image_name\":\"http:\\/\\/www.mcfinder.ie\\/admin\\/images\\/attributes\\/t_cashless_icon.gif\"},{\"attribute_name\":\"McDrive\",\"image_name\":\"http:\\/\\/www.mcfinder.ie\\/admin\\/images\\/attributes\\/car_icon.png\"}]}]");
JSONObject object = outerArray.getJSONObject(0);
JSONArray attributes = object.getJSONArray("attributes");
for (int i = 0, length = attributes.length(); i < length; i++)
{
JSONObject attribute = attributes.getJSONObject(i);
System.out.printf("attribute name=%s, image=%s\n", attribute.getString("attribute_name"), attribute.getString("image_name"));
}
}
}
If you're not stuck using the built-in JSON API that Android provides, I highly recommend switching to Jackson, which makes it very easy to read and write arbitrarily complex JSON with Java. Following is an example of using it.
import java.io.File;
import java.math.BigDecimal;
import java.net.URI;
import java.util.List;
import org.codehaus.jackson.map.ObjectMapper;
public class Foo
{
public static void main(String[] args) throws Exception
{
ObjectMapper mapper = new ObjectMapper();
Store[] stores = mapper.readValue(new File("input.json"), Store[].class);
Store store = stores[0];
List<Attribute> attributes = store.attributes;
for (Attribute attribute : attributes)
{
System.out.printf("attribute name=%s, image=%s\n", attribute.attribute_name, attribute.image_name);
}
// output:
// attribute name=Wi-Fiiiii, image=http://www.mcfinder.ie/admin/images/attributes/t_wifi_icon.gif
// attribute name=Cashless, image=http://www.mcfinder.ie/admin/images/attributes/t_cashless_icon.gif
// attribute name=McDrive, image=http://www.mcfinder.ie/admin/images/attributes/car_icon.png
}
}
class Store
{
public String store_id;
public String store_name;
public String store_type;
public String vouchers_available;
public String store_limit;
public String distance;
public BigDecimal latitude;
public BigDecimal longitude;
public URI image_name;
public int voucher_count;
public String is_open;
public List<Attribute> attributes;
}
class Attribute
{
public String attribute_name;
public URI image_name;
}
The answer by #Richard aka cyberkiwi for this question should be the answer for your question.
He says:
You may be passing the STRING to JSONObject with leading spaces. Try trimming
JSONObject allCDs = new JSONObject(objectString.replace(/^\s+/,""));
EDIT: I thought this was javascript. Try trimming it using Java code instead
JSONObject allCDs = new JSONObject(objectString.trim());
If that still doesn't work, then show what the first character from the string is:
System.out.println((int)objectString.trim().charAt(0));
You should be expecting 123, the curly braces. In fact, check the entire content
System.out.println((int)objectString); // or
System.out.println((int)objectString.trim());
You could also try cutting everything before the { in the string
JSONObject allCDs = new JSONObject(objectString.substring(objectString.indexOf('{')));
If you're taking the Udacity android course and encountering this error for the quakereport/DidUfeelIt app then change the URL and try with some other URL your problem will be solved. Eg:- The URL provided by during the course was "http://earthquake.usgs.gov/fdsnws/event/1/query?format=geojson&starttime=2012-01-01&endtime=2012-12-01&minmagnitude=6"
Then I was getting the same error that is "problem parsing the JSON" So I tried different URL: https://earthquake.usgs.gov/earthquakes/feed/v1.0/summary/4.5_day.geojson
And it worked..!! Always try to get the latest URL's from the USGS website during the course.

efficient way to parse the file to get a List back ...?

ID Name Department Salary Designation
---------------------------------------
1 Kent Engineering 100000$ senior-engineer
2 Smith null 50,000$ administrator
These are the records of a big file I have. I need to parse this file to retrieve salary and designation. The key has to be ID,Name,Department and if that fails then use ID,Name ..
I can only use Java/groovy to do this... How do I get <Salary,Designation> back... ?
What's the most efficient way to grep it given that i can use only java/groovy
Create a BufferedReader, this will allow you to read the file one line at a time. Then call split("[\\\s]") on each of the lines, this will split it by whitespace.
You may also have to massage the salary field so it can be parsed into a number.
Split each line on whitespace.
scala> val a = "1 Kent Engineering 100000$ senior-engineer"
a: java.lang.String = 1 Kent Engineering 100000$ senior-engineer
scala> a.split("\\s+")
res1: Array[java.lang.String] = Array(1, Kent, Engineering, 100000$, senior-engineer)
Then take the raw string values and parse those. E.g. to convert 50,000$ into the number 50000:
double salary = Double.parseDouble(array[SALARY_INDEX].replaceAll("[$,]",""))
java.lang.Double.parseDouble("50,000$".replaceAll("[$,]",""))
res6: Double = 50000.0
You can parse the file into a HashMap and keep it around as long as you need it.
It has a high up-front cost, but if you're going to use it a lot, then it makes sense.
Then create your key and value objects. Be sure to override equals for your key.
public class Key{
int id;
String name;
#Override
public boolean equals(Object o){...}
}
I'll assume that you're aware of how to read files line-by-line using a BufferedReader as that's an obvious prerequisite for this sort of thing - if not, shout.
The key is the thing that you've failed to specify. What, exactly, is the format of the data - in particular, what are the exact rules for determining where one field ends and the other begins?
If the data is separated by tab characters (and said characters are forbidden from occurring in the data, even if escaped) then the solution is simple:
// Ignoring general error handling and EOF-checking here
final String line = bufferedReader.readLine();
final String[] fields = line.split("\t");
Now you have an array containing the fields on the line, so can just look up fields[3] and fields[4].
If the separator is "any number of spaces", and spaces are entirely disallowed, then you've got a similarly easy situation, where your regex is line.split(" *").
In more complex situations, including ones where separators are allowed to appear if escaped or quoted, you may be better off simply iterating over the line character-by-character and manually building up the set of fields according to the separator rules.
With a pinch of Guava library, the solution is simple and elegant. We can improve the below code by objectifying the key, handling errors etc etc but you can do that yourself
import java.io.File;
import java.io.IOException;
import java.util.Iterator;
import java.util.Map;
import com.google.common.base.CharMatcher;
import com.google.common.base.Charsets;
import com.google.common.base.Joiner;
import com.google.common.base.Splitter;
import com.google.common.collect.Maps;
import com.google.common.io.Files;
import com.google.common.io.LineProcessor;
public class FileProcessor
{
private static final Splitter SPLITTER = Splitter.on(CharMatcher.WHITESPACE);
private static final Joiner KEY_BUILDER = Joiner.on("_").skipNulls();
#SuppressWarnings("unchecked")
public static void main(final String[] args) throws IOException
{
Map<String, SalaryAndDesignation> result = Files.readLines(new File("c:/1.txt"), Charsets.ISO_8859_1, new LineProcessor() {
private final Map<String, SalaryAndDesignation> result = Maps.newHashMap();
public Object getResult()
{
return result;
}
public boolean processLine(final String line) throws IOException
{
Iterator<String> columns = SPLITTER.split(line).iterator();
String id = columns.next();
String name = columns.next();
String dept = columns.next();
String key = KEY_BUILDER.join(id, name, "null".equals(dept) ? null : dept);
result.put(key, new SalaryAndDesignation(columns.next(), columns.next()));
return true;
}
});
System.out.println(result.size());
}
final static class SalaryAndDesignation
{
String salary;
String designation;
public SalaryAndDesignation(final String salary, final String designation)
{
super();
this.salary = salary;
this.designation = designation;
}
}
}
I see using groovy results in lesser code as sample below (copy/pasted from here) but never used it myself
new File("simple.tab").withReader{r->
line = r.readLine();
println "first line: $line"
r.splitEachLine("\t"){fields->
println "fields on line: $fields"
}
}

Categories

Resources