2

I am parsing a json object like this:

val product_array:Option[Any] = scala.util.parsing.json.JSON.parseFull(products_json)

var product_array2 = Array()

product_array match {
case Some(p) => {
for {
  (id, desc) <- p.asInstanceOf[Map[String,Map[String,Any]]]
  (propName, propValue) <- desc
}  product_array2(id) ++ Array(propName->propValue.toString)
}
case None => test = "No products in shopping cart"
}

The problem is that I am trying to create a multidimensional array with this line:

product_array2(id) ++ Array(propName->propValue.toString)

But it doesn't work. How can I create a multidimensional array in the for loop?

Hopefully I can clarify:

In PHP it would be this:

product_array2[id][propName]=propValue;
2
  • 1
    Please be more clear what the intended outcome is. You want a multidimensional array, fine. But what is stored in which dimension? For example product_array2(id) does not make sense, because id is a String. My guess is you want to convert id to an integer and use that as the outer array dimension. But it is not clear from you current formulation. Commented Jun 29, 2012 at 17:16
  • 2
    PHP's Array is not what anyone else would recognize as an array. It's what's generally called a dictionary or a map, and it is called a Map in Scala. Commented Jun 29, 2012 at 18:18

2 Answers 2

2

If you want to access the structure, the answer of @lpaul7 will tell you how. If you want to update the structure, you either need to deal with the nested immutable Map, or convert the result indeed into a mutable structure (what you tried to do with the Array).

This gets all a bit nasty:

import scala.util.parsing.json.JSON._

val json = """{"id": {"age": 33}}"""

val im = parseFull(json) match {
  case Some(m: Map[_, _]) => 
    m.asInstanceOf[Map[String, Any]].collect {
      case (key, value: Map[_, _]) => (key, value.asInstanceOf[Map[String, Any]])
    }
  case _ => Map.empty[String, Map[String, Any]]
}

Query:

im("id")("age")

Immutable update:

val updated = im + ("id" -> (im.getOrElse("id", Map.empty) + ("age" -> 44)))

Mutable structure (using mutable maps both for the outer and inner structure):

import collection.{breakOut, mutable}

val mut: mutable.Map[String, mutable.Map[String, Any]] = 
   im.map { case (key, inner) => key -> mutable.Map(inner.toSeq: _*)} (breakOut)

Mutable update:

mut("id")("age") = 55

In other words, you will really want a noise-free Scala JSON solution, such as the Lift's json, sjson, or Jerkson libraries.

Sign up to request clarification or add additional context in comments.

Comments

1

You can achieve it easily:

import scala.util.parsing.json.JSON._

val products_json = """{"p...67890"}}"""

val product_array = 
  parseFull(products_json).get.asInstanceOf[Map[String,Map[String,Any]]]

println(product_array("product1Id")("product_name"))

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.